Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python tip of the week #4

Open
9 of 14 tasks
alastair opened this issue Dec 11, 2018 · 5 comments
Open
9 of 14 tasks

Python tip of the week #4

alastair opened this issue Dec 11, 2018 · 5 comments

Comments

@alastair
Copy link
Member

alastair commented Dec 11, 2018

Some suggestions of tips to email people about how to do things better in python

  • Use python 3
  • How to do stylechecks using your favourite editor
  • Use time.monotonic() to count how long something takes
  • Use concurrent.futures for multiprocessing
    [ffont] I have a utility class for this in pymtg based in concurrent.futures. we can talk about that. I guess I could do this tip
  • Use sets or dictionary keys to check membership when in loops
  • collections.defaultdict
  • collections.Counter
  • os.path.join or Pathlib instead of concatenating paths with + '/' +
  • Argument parsing / Fire
  • CSV module instead of constructing by hand
  • enumerate
  • dict.items
  • zip
  • sorting
@LPorcaro
Copy link

I like to use pyflakes for checking my code while testing
https://pypi.org/project/pyflakes/
Maybe it can be in the same tip with stylechecks (even if it is not properly a stylecheck)

@jordipons
Copy link

jordipons commented Dec 19, 2018

student_tuples = [ ('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10), ]
sorted(student_tuples, key=lambda student: student[2]) # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

Sort with the lambda function.
https://wiki.python.org/moin/HowTo/Sorting

@alastair
Copy link
Member Author

sorting is a great item! especially using lambdas or the operator module for the key parameter.

@xavierfav
Copy link

xavierfav commented Dec 19, 2018

More suggestions:

  • *args and **kwargs
  • Avoid running out of memory with Generators
  • for/else
  • Classes
  • Reduce RAM usage & make attribute accesses faster with __slots__ (https://habr.com/en/post/458518/)
  • Variable scope

@wuaalb
Copy link

wuaalb commented Dec 19, 2018

  • conda package/environment manager
  • collections.OrderedDict (note dict ordered in CPython 3.6 and dict ordered in 3.7 language)
  • f-strings (3.6+)
  • itertools (e.g. example below)
  • numba jit compiler
  • pysoundfile (libsndfile wrapper) for audio file i/o
  • resampy for resampling
  • pytoml / TOML for config files
  • logging

example of splitting note sequence into phrases using itertools.groupby:

>>> import itertools
>>> notes = ['sil', 'g', 'g', 'd', 'd', 'e', 'e', 'd', 'sil', 'c', 'c', 'b', 'b', 'a', 'a', 'g', 'sil']
>>> phrases = []
>>> for is_sil, group in itertools.groupby(notes, key=lambda note: note == 'sil'):
...     if not is_sil:
...         phrases.append(list(group))
... 
>>> print(phrases)
[['g', 'g', 'd', 'd', 'e', 'e', 'd'], ['c', 'c', 'b', 'b', 'a', 'a', 'g']]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants