Giter VIP home page Giter VIP logo

python-cheatsheet's People

Contributors

crazyiop avatar dmitri-mi avatar gto76 avatar imba-tjd avatar jared-hughes avatar ri0t avatar s-weigand avatar ssarcandy avatar theconsultant avatar tilboerner avatar tweakimp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-cheatsheet's Issues

Virtual environment, tests, setuptools and pip

I love this cheat sheet!
It would be great to add some points about how to make production-grade code, such as:

  • virtual environment (e.g., with virtualenvwrapper)
  • test (unittest, pytest, mock, tox)
  • packaging and installation (setuptools, pip, ...)

Some points about how "import" works would be great too (one of the most used yet not understood feature in Python)
I realize that those are quite broad topics (especially the last one), but that would sure be very helpful

Add a LICENSE.txt

Add a LICENSE.txt file in the root of the repo, with an appropriate license (my vote would be for MIT, BSD, Apache, or CC-BY)

TODO

Cheatsheet

  • MemoryView
  • Pathlib
  • Asyncio
  • Datetime
  • Logging
  • Dataclass

Page

  • Static page (Automatic generation of index.html from README.md at commit)
  • Better mobile experience (Help needed)
  • Back to top button (Help needed)

Printable PDF

Would be nice to have nice printable PDF for the wall hangers enthusiasts 🥇

Licence file

Hello,
I think adding a licence file would be useful as the actual licence of the project is a bit ambiguous (it is by default not possible legally to use any of the code in the cheatsheet).
Is it on purpose ?

Thank you

Bytes encoding and decoding

I learned that there are two ways to encode and decode bytes. One has been included:

<bytes> = <str>.encode(encoding='utf-8')
<str>   = <bytes>.decode('utf-8') 

the other is

<bytes> = bytes(<str>,encoding='utf-8')
<str> = str(<bytes>,encoding='utf-8')

Just to remind:-D. May be you just choose the one you prefer~

Exceptions need more details

Firstly, nice doc.

For exceptions, having a comment on which is executed for what failures would be good for else and finally

List flattening does not work properly

import itertools
nested_list1 = ["s", "r", [32, [32], 3]]
flattened_list1 = list(itertools.chain.from_iterable(nested_list1))
print(flattened_list1)
nested_list2 = [1, [1, 1, 3, [224, 4], []], [], [2, [23]]]
flattened_list2 = list(itertools.chain.from_iterable(nested_list2))
print(flattened_list2)
# RESULT
# ['s', 'r', 32, [32], 3] (flattened_list) <-- not completely flat
# TypeError: 'int' object is not iterable (flattened_list2)  <-- doesnt work at all

I suggest:

from copy import deepcopy
def flatten(nested):
    """Flatten an arbitrarily nested list."""
    nested = deepcopy(nested)
    flat = []
    while nested:
        sublist = nested.pop(0)
        if isinstance(sublist, list):
            nested = sublist + nested
        else:
            flat.append(sublist)
    return flat
list1 = flatten(nested_list1)
list2 = flatten(nested_list2)

print(list1)
print(list2)
# RESULT
# ['s', 'r', 32, 32, 3] (list1)
# [1, 1, 1, 3, 224, 4, 2, 23] (list2)

More elegant way to calculate the product of the elements

In python 3.8 there is a more elegant way to calculate the product of the elements.

Instead of
product_of_elems = functools.reduce(lambda out, el: out * el, <collection>)
it can be done like this
product_of_elems = math.prod(<collection>)

Python

Full python code cheats

Bravo

The codes are superb!

Definition of any()

The definition of any(collection) says "False if empty", but that's not the whole story.

any( [ False ] ) is False even though the argument is not empty.

Datetime

from datetime import datetime, strptime
now = datetime.now()
now.month                     # 3
now.strftime('%Y%m%d')        # '20180315'
now.strftime('%Y%m%d%H%M%S')  # '20180315002834'
<datetime> = strptime('2015-05-12 00:39', '%Y-%m-%d %H:%M')

The first line has a typo and should be from datetime import datetime, strftime.
But you can't import strftime from the root module.

datetime.datetime.now() returns a datetime object, and those can be called with strftime I.E.

from datetime import datetime
datetime.now().strftime('%b')
# --> Feb

Simply the first line just needs the last word deleted off. Don't forget the typo in the last line either ;)

I'd take this opportunity to mention a few cool tricks about from datetime import date but I'm at work right now. If I get a chance later I may open a pull request.

Brilliant stuff

I've seen tons of "cheat sheets" but this one is different. I'm still trying to figure how to to make it part of my coding environment. I may add some tools around it, like I did a tool called "How Do I" that queries Stack Overflow. I made a GUI. I may make a GUI for this too or something that allows easy access from PyCharm so I never leave my coding environment.

I dunno yet exactly how to integrate this amazing documentation, but, I do know it's a true gem of a find.

Thanks very much!

No issue at all. I just wanted to thank you for making your cheatsheet available. Really comprehensive and helpful – very much appreciated! 👍

reformat and lint python cheatsheet

Read thru the source, and in the process markdown linted and reformated the source for readability.
I don't know how/if I can upload the git commit, so I attached source file here.
Feel free to use any of my suggested changes to the source.
Thanks
README.md

List comprehension - order of loop variables

In this section:


out = [i+j for i in range(10) for j in range(10)]

Is the same as:

out = []
for i in range(10):
    for j in range(10):
        out.append(i+j)

The loop variables i and j should be swapped in either the first or the second part. Maybe one of the 10s could even be changed into another value to make it clearer? For instance:

out = [i+j for j in range(5) for i in range(10)]

Is the same as:

out = []
for i in range(10):
    for j in range(5):
        out.append(i+j)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.