Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you manage script?
#11
@rob101 thank you for the info, that is the one I looking for,
i added reputation point for you,
anyway, i don't know why the PM do not work, maybe you will not receive my messages or get spammed of my messages, if that happen, sorry
rob101 likes this post
Reply
#12
(Oct-13-2022, 06:34 PM)kucingkembar Wrote: @rob101 thank you for the info, that is the one I looking for,
i added reputation point for you,
anyway, i don't know why the PM do not work, maybe you will not receive my messages or get spammed of my messages, if that happen, sorry

The PM worked just fine.

Thank you. I'm sure you'll find that excellent app to be very useful.
kucingkembar likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#13
(Oct-13-2022, 04:01 PM)kucingkembar Wrote: because if in the future I need it again,
I do not need to google-ing again,
I just need it from some the previous "subscripts" that I have googled previously
currently, I put that in a txt file, and over 100 "subscripts"
it does not just "always available" function

So what are advantages of looking for subscripts in your file compared to built-in help? In both cases you must know what you are looking for and built-in help is always available. Instead of googling you can go directly to Python official documentation.

Furthermore, if examples you provided are representative of your 'subscripts' (these are usually called code snippets or just snippets) then you just scratched the surface of knowledge.

Let's take your first 'subscript':

#trim whitespace start and end
txt = "     Hello!  "
strippedtxt = txt.strip()
print(strippedtxt)
string strip method is not about trimming whitespace at start and end. There is no indication that strip takes arguments. There is no hints what strip does (following is from documentation):

str.strip([chars])

Quote:Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:

>>> '   spacious   '.strip()
'spacious'
>>> 'www.example.com'.strip('cmowz.')
'example'
The outermost leading and trailing chars argument values are stripped from the string. Characters are removed from the leading end until reaching a string character that is not contained in the set of characters in chars. A similar action takes place on the trailing end. For example:

>>> comment_string = '#....... Section 3.2.1 Issue #32 .......'
>>> comment_string.strip('.#! ')
'Section 3.2.1 Issue #32'
In my mind, if I would take the snippets approach I would write:

# str.strip([chars])
# remove leading and trailing characters
# chars argument is a string specifying the set of characters to be removed
# if chars argument is omitted or None removes whitespaces

>>> '   spacious   '.strip()
'spacious'
>>> 'www.example.com'.strip('cmowz.')
'example'

# Beware! Characters are removed until reaching a character that is not in chars argument
# can't be reliably used to strip web addresses:

>>> 'www.walmart.com'.strip('w.com')
'almart'

# https://docs.python.org/3/library/stdtypes.html#str.strip
# for removing only from left or right use lstrip and rstrip
This way I have knowledge how to remove any character, not just whitespace. It's abstraction and it's very important in programming. If you know how to remove characters you also know how to remove whitespaces. If you know how to remove whitespaces you don't know how to remove other characters. If you write for every character in alphabet a 'subscript' then it will be really hard to manage.

Regarding finding 'subscripts'. I use ack which is described as "source code search tool". I use it not for finding subscripts but looking for code I have written in the past.

Practical example - I remember, that divmod is used to solve specific problem (quotient and reminder) and I have feeling that in order to solve problem in hand I need it. I know that I have used it previously, so I look for usage of divmod in my files:

+ > ack divmod
calculate_change.py
6:        quantity, amount = divmod(amount, value)

cash_machine.py
10:        quantity, amount = divmod(amount, value)
So I know that there are two files that contain that code. I then could expand it, to get context (five rows before and after the row containing divmod):

+ > ack -C 5 divmod
calculate_change.py
1-"""Calculate change."""
2-
3-def change(amount, units={25: 'quarter', 10: 'dime', 5: 'nickel', 1: 'pennie'}):
4-    quantities = dict()
5-    for value, name in units.items():
6:        quantity, amount = divmod(amount, value)
7-        if quantity:
8-            quantities[name] = quantity
9-        if not amount:
10-            break
11-

cash_machine.py
5-def change(amount, denominations):
6-    if amount == 0:
7-        return dict.fromkeys(denominations, 0)
8-    quantities = dict()
9-    for value, name in denominations.items():
10:        quantity, amount = divmod(amount, value)
11-        quantities[value] = quantity
12-        if not amount:
13-            break
14-
15-    return quantities
I inspect the code I have written in the past, I remember how I used it and then (if applicable) apply this knowledge to solve problem at hand (beware, that this example is search from my temp folder so I don't know whether these are actually working or not and I am too lazy to find out).

Files might be long (hundreds or thousands of lines) and sometimes it's not enough to inspect context. In these cases I open the file on specific line or function using vim.
Larz60+ likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#14
I think you forgot this thread started with
Quote:sorry for my bad English
.
Even well written documentation is not helpful if you can't read it. Is Python help localized?

Some of the code snippets may address more advanced topics, or document tips and tricks that are difficult to look up. I have a program named "monkeypatch.py" that I wrote two years ago. It does a great job demonstrating how Python organizes information in classes, how to use inspect, how to "compile" code and inset it into class, how dunders work, etc... I pull it out frequently to refresh my memory, and I find it far more useful that google.

On the flip side it is dangerous using your own code snippets as your primary Python reference. This is code you wrote when you didn't know much about Python. How much of it is crap? How much it is misleading. I still reference my monkeypatch example, but it has changed over time. As I get better, it gets better. I don't think that would happen if I had hundreds of little snippets. A dozen or so can be maintained this way.
kucingkembar likes this post
Reply
#15
(Oct-13-2022, 04:35 AM)kucingkembar Wrote: so, any tips, software, or idea to handle this?
I use a program that I wrote myself to manage not only code snippets but innumerable small pieces of information. It is a command line application which basically queries a small database (currently a json file). The name of the program is kno-g, so for example if I type this in a terminal

Output:
$ kno-g warnings
The program shows me a list of all the notes that contain the word 'warnings'. In this case there are only 2 notes as you can see in the attached screenshot.

Then I can choose to view the note or to edit it, which opens a text editor to do so and I see the contents of the note.

Each note has a first line with a question, which is used to create the list of notes to choose from, and it has a body, the answer to the question. For example here is one note (in french, the question is how to remove all warnings from python)

Output:
Comment supprimer tous les warnings de python ? mettre tout en haut import warnings warnings.filterwarnings("ignore") aussi en ligne de commande: python -W ignore
The search engine is extremely simple: for every word in the notes, the program keeps a reference of all the notes that contain this word.

I have been using this program for many years, I use it every day and as simple as it is, it saved me a lot of time and efforts.
kucingkembar likes this post

Attached Files

Thumbnail(s)
   
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python manage variables across project level mg24 1 926 Nov-12-2022, 05:01 AM
Last Post: deanhystad
  Python, how to manage multiple data in list or dictionary with calculations and FIFO Mikeardy 8 2,620 Dec-31-2021, 07:47 AM
Last Post: Mikeardy
  how to manage crypto trading flooding data from exchange servers Mikeardy 0 1,245 Dec-26-2021, 08:31 PM
Last Post: Mikeardy
  API Gateway to manage multiple API's get put data robsuttonjr 2 2,554 Mar-09-2021, 04:09 PM
Last Post: robsuttonjr
  Manage user names in TRAC susja 3 6,011 Dec-06-2020, 09:34 PM
Last Post: susja
  How to manage multiple datasets in Python ThePhantom 2 1,935 May-06-2020, 12:17 AM
Last Post: ThePhantom
  how to manage an input() rodrigoH 1 1,646 Mar-16-2020, 12:24 AM
Last Post: jefsummers
  Easier way to manage dependencies? Tylersuard 2 2,364 Jan-01-2020, 09:19 PM
Last Post: Tylersuard

Forum Jump:

User Panel Messages

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