Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pyutils module
#11
Well...  quite a lot of things about that code aggravate me; most of them could be solved by making it pep8 compliant.

The main things are trying to one-line functions even when they are excessively long.
Also, you should be using docstrings for documentation, not #comments.  
#comments should be used for inline comments, or to explain specifically convoluted things that occur in the middle of a function.

For a primary example of things that annoy me take this function:
def isnum(x):           return True if 'bit_length' in dir(x) else True if 'is_integer' in dir(x) else True if 'radix' in dir(x) else False
This is onelined just for the sake of it and is using multiple ternaries to do so.

Even more so, I think you will find that this does the same thing:
from numbers import Number

#...

isinstance(thing, Number)
In fact it looks like almost all of those functions in that block could be handled with isinstance. There is no need to check the dir of an object to tell if it is a dict or not (unless you are worried about subclasses of dict being positives in which case your method won't work either).
>>> a = []
>>> b = {}
>>> isinstance(a, dict)
False
>>> isinstance(b, dict)
True
>>>
#12
(Oct-08-2016, 02:24 AM)Mekire Wrote: Well...  quite a lot of things about that code aggravate me; most of them could be solved by making it pep8 compliant.

The main things are trying to one-line functions even when they are excessively long.
Also, you should be using docstrings for documentation, not #comments.  
#comments should be used for inline comments, or to explain specifically convoluted things that occur in the middle of a function.

For a primary example of things that annoy me take this function:
def isnum(x):           return True if 'bit_length' in dir(x) else True if 'is_integer' in dir(x) else True if 'radix' in dir(x) else False
This is onelined just for the sake of it and is using multiple ternaries to do so.

Even more so, I think you will find that this does the same thing:
from numbers import Number

#...

isinstance(thing, Number)
In fact it looks like almost all of those functions in that block could be handled with isinstance. There is no need to check the dir of an object to tell if it is a dict or not (unless you are worried about subclasses of dict being positives in which case your method won't work either).
>>> a = []
>>> b = {}
>>> isinstance(a, dict)
False
>>> isinstance(b, dict)
True
>>>

i am learning here.  a lot of my code is styled the way i have coded in the past.  but i am certainly open to new ways. i can adopt them faster if they can be done in all languages.  for example using only spaces and never tabs.  that works in all languages i code in.   tabs save file space but really is no issue these days. tabs save transmission time but that is no issue with broadband even at slow US speeds.  and where these are issues we have good compression that is fast on today's computers.

keep the feedback coming.  i learn more from it.  a lot of this is more than just learning the code or even how to combine different things ... it is also about learning what others prefer.

one-line functions were more about lining things up vertically.  no benefit by this? OK

i wish it were easy to break up the quote so i can respond to each issue separately. maybe MyBB has a means to insert text in a big quote and make it really look like a followup.

i did not know docstrings could be done multiple times.  how should i put licensing in?  docstrings too?

OK, so numbers.Number is the preferred way?  can you suggest better stuff for the other things, too?  i'll be switching things over to isinstance() (which means ultimately getting rid of those is*() functions).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
#13
You can see an example of my doc style here:
https://github.com/Mekire/pygame-samples..._change.py

Docstrings will actually show up when you run help on something so adopting the convention of using them actually does have some merit. 

For example with the above file:
>>> import color_change
>>>
>>> help(color_change)
Help on module color_change:

NAME
    color_change

FILE
    c:\users\sean\desktop\programming\pythonstuff\pygame-samples\color_change.py

DESCRIPTION
    This example serves as a basics introduction.
    Change the screen color with a mouse click.

    This program is intentionally over-commented to serve as an introduction for
    those with no knowledge of pygame. More advanced examples will not adhere to
    this convention.

    -Written by Sean J. McKiernan 'Mekire'

CLASSES
    __builtin__.object
        App

    class App(__builtin__.object)
     |  This is the main class for our application.
     |  It manages our event and game loops.
     |
     |  Methods defined here:
     |
     |  __init__(self)
     |      Get a reference to the screen (created in main); define necessary
     |      attributes; and set our starting color to black.
     |
     |  event_loop(self)
     |      Our event loop; called once every frame.  Only things relevant to
     |      processing specific events should be here.  It should not
     |      contain any drawing/rendering code.
     |
     |  main_loop(self)
     |      Our game loop. It calls the event loop; updates the display;
     |      restricts the framerate; and loops.
     |
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     |
     |  __dict__
     |      dictionary for instance variables (if defined)
     |
     |  __weakref__
     |      list of weak references to the object (if defined)

FUNCTIONS
    main()
        Prepare our environment, create a display, and start the program.

DATA
    CAPTION = 'Click to Change My Color'
    SCREEN_SIZE = (500, 500)
#14
is there an easy way (i know a couple hard ways) to redirect the output of help() to a file?   file= does not work.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
#15
victor@jerry:~/Programming$ python
Python 2.7.12 (default, Jul  1 2016, 15:12:24) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout = open('testfile.txt', 'w')
>>> help(sys.stdout)
>>> 
After some googling I found that this will mess  the print output also.
This returns it back to normal
sys.stdout = sys.__stdout__
But is better to use with
with open('testfile.txt', 'w') as f:
    sys.stdout = f
    # redirection 
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
#16
(Oct-08-2016, 07:05 AM)Skaperen Wrote: is there an easy way (i know a couple hard ways) to redirect the output of help() to a file?   file= does not work.

Pydoc has an option to output the documentation as an html file.
$ python -m pydoc -w color_change
wrote color_change.html
Or just send it to a file as normal (maybe slightly different syntax on linux; I forget):
$ python -m pydoc color_change > "documentation.txt"


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyutils.py Skaperen 8 6,055 May-09-2020, 01:22 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020