(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.