Im going through the book Dive into Python 3 and in one of its parts theres a .py file which contains a function that has a triple quoted string that as the book explains it, is a document.
When I go into the python shell and import said file and write
print(humansize.approximate_size.__doc__)
it gives me back the said triple quoted string.
I decided Id give it a try myself and created a triple quoted string right under the other one. Saved the file and ran the same code in the python shell - but only the first one appeared. Why is that?
Do i need to install some separate tool to document code ?
Thank you!
Related
I wanted to learn command line programming using Python.
I saw a to-do challenge on the internet and started to work on it by learning from the web. The challenge is to create a command line interface of a to-do app.
The challenge is titled CoronaSafe Engineering Fellowship Test Problem. Here is the challenge material on Google Drive: https://drive.google.com/drive/folders/1SyLcxnEBNRecIyFAuL5kZqSg8Dw4xnTG?usp=sharing
and there is a GitHub project at https://github.com/nseadlc-2020/package-todo-cli-task/
In the README.md I was instructed to create symbolic link for the batch file todo.bat with the name todo. Now, my first condition is that, when the symbolic link is called from the command prompt without any arguments, it must print some usage tips for the program. Finally, I have to use the npm test command to test the execution.
At the very beginning I got this trouble, whenever I use a print statement, I see a dot • at the end of every string which ends with a new line. For instance,
import sys
import random
args = sys.argv[1:]
if len(args) == 0:
print('Usage :-', end='\n')
print('$ ./todo help # Show usage', end='')
The above statements when executed without arguments gives the output,
Usage :-.
$ ./todo help # Show usage
Here, I noticed that for the first print statement ends with a newline, the string ends with what looks like a middle dot (•). Whereas, for the second print statement since I override the end parameter with an empty string, no newline character was output, and so the dot is not printed. See the screen shot:
What's wrong, and how can I pass the test? My program does not print a middle dot at all.
The problem seems to be squarely inside the todo.test.js file.
In brief, Windows and Unix-like platforms have different line ending conventions (printing a line in Windows adds two control characters at the end, whilst on Unix-like systems only one is printed) and it looks like the test suite is only prepared to cope with results from Unix-like systems.
Try forcing your Python to only print Unix line feeds, or switch to a free Unix-like system for running the tests.
Alternatively, rename todo.test.js and replace it with a copy with DOS line feeds. In many Windows text editors, you should be able to simply open the file as a Unix text file, then "Save As..." and select Windows text file (maybe select "ANSI" if it offers that, though the term is horribly wrong and they should know better); see e.g. Windows command to convert Unix line endings? for many alternative solutions (many of which vividly illustrate some of the other issues with Windows; proceed with caution).
This seems to be a known issue, as noted in the README.md you shared: https://github.com/nseadlc-2020/package-todo-cli-task/issues/12 (though it imprecisely labels this as "newline UTF encoding issues"; the problem has nothing to do with UTF-8 or UTF-16).
See also the proposed duplicate Line endings (also known as Newlines) in JS strings
I had exactly the same problem.
I replaced:
print(variable_name) # Or print("Your text here")
With:
sys.stdout.buffer.write(variable_name.encode('utf-8')) # To sys.stdout.buffer.write("Your text here".encode('utf-8'))
Now it worked fine in windows.
First write your help string like this
help_string='Usage :-\n$ ./task add 2 hello world # Add a new item with priority 2 and text "hello world" to the list\n$ ./task ls # Show incomplete priority list items sorted by priority in ascending order\n$ ./task del INDEX # Delete the incomplete item with the given index\n$ ./task done INDEX # Mark the incomplete item with the given index as complete\n$ ./task help # Show usage\n$ ./task report # Statistics'
Then print it on the console using
sys.stdout.buffer.write(help_string.encode('utf8'))
This problem occurs due to differences in encoding type of windows and npm tests. Also make sure to avoid any spaces after or before "\n".
Why have multiple prints,when python prints can incorporate new line without having to declare separately, follow example below:
print("Usage :- \n$ ./todo help #Show usage")
Output:
Usage :-
$ ./todo help #Show usage
I have a python program that reads a csv file, makes some changes, then writes to an HTML file. The issue is a block of code where I'm trying to search for a string assigned to one variable, then replace it with another string assigned to another variable. I am able to read a line in the csv file that looks like this:
Link:,www.google.com
And I am successful in writing an html file with the following:
<tr><td>Link:</td><td>www.google.com</td></tr>
Essentially I want to go further with an added step to find www.google.com between the anchor tags and replace it with "GOOGLE".
I've researched 'find and replace' functions built into python, and I came up with the substitution function inside the regular expressions module (re.sub()). This might not be the best way to do it and I'm trying to figure out if there's a better function/module out there I should look into.
python
for line in file:
newHTML.write(re.sub(var1,var2,line,flags=re.MULTILINE), end='')
newHTML.write(re.sub(var3,var4,line,flags=re.MULTILINE), end='')
The error I am receiving is:
newHTML.write(re.sub(var1,var2,line,flags=re.MULTILINE), end='')
TypeError: write() takes no keyword arguments
If I comment out this code, the rest of the program runs fine albeit without finding and replacing these variables.
Perhaps re.sub() doesn't go well with write()?
The error says what the problem is: as #furas commented, write() is not the same as print(), and doesn't accept the end='' keyword argument. file.write() by default doesn't include newlines if you don't explicitly put any \n's, so it should work if you change the line to:
newHTML.write(re.sub(var1,var2,line,flags=re.MULTILINE))
Also, regex and HTML aren't the best of friends... Your case is simple enough that using regex is fine, but you mentioned looking for a better module to generate HTML. This SO question had some good suggestions in the answers. Notable mentions for creating HTML templates on there were xml.etree, jinja2 (Flask's default engine), and yattag.
I want to write a function to process some data I bring from Excel. The data is essentially in an Excel column (transaction IDs). For reasons of my own convenience, I thought I'd use raw_input with copy-pasting the column from Excel, store it and run the function on that.
However, whatever I do I get errors (I actually got stuck in this very first stage of bringing in the data), and I'm pretty sure the reason is because each item is in a new line (when I use Excel's option to transpose the column to a row, I get no errors).
So, for instance, if I wanted to try and set a sample string to work with, e.g.:
some_string = "014300071432Gre
014300054037Col
014300065692ASC"
(this is the formatting you get when pasting from a column in Excel),
and just call some_string, I'd get:
File "<stdin>", line 1
al = "014300071432Gre
^
SyntaxError: EOL while scanning string literal
I tried removing the line-breaks with .split() but that didn't work
I also tried the triple quotes I saw suggested in several threads, but that didn't work either. It only got me more confused because I thought triple quotes are used when you don't want python to evaluate something.
I've placed some Sample Data in a Google doc.
Would really appreciate any help.
Thanks!
You're right that the difficulty in using raw_input with a copied column of Excel data is the newlines. The issue is that raw_input specifically reads one line. From the official docs:
raw_input([prompt])
If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.
By definition, a newline character marks the end of a line. So there really isn't a simple way to paste a column of Excel data into raw_input.
In most cases, the best way to read Excel data from Python is simply to read the Excel file directly. The best package for this is xlrd. Assuming your workbook is named myData.xls and you want to read A2:A5 from the first sheet, you would do something like
import xlrd
wb = xlrd.open_workbook('myData.xls')
ws = wb.sheet_by_index(0)
result = ws.col_values(0, 1, 5)
At that point, result would be a 4-element list of cell values (A2, A3, A4, and A5).
If you really need the user interface to be "copy a range of cells in Excel; paste into my app" then you probably have to look into building a GUI which has a multiline text input box. Here you have lots of choices, from Python's included Tkinter, to third-party libraries for Python, to non-Python GUIs (as long as they can read the input and then pass it to your Python program).
Edit: You can read the clipboard directly (so don't do the paste step at all). See these questions for more information. The simplest solution taken from those questions relies on Tkinter:
from Tkinter import Tk
r = Tk()
result = r.selection_get(selection='CLIPBOARD')
r.destroy()
The above assumes that the clipboard is already populated. In other words, the flow would be something like
Your program prompts the user to copy a selection in Excel
The user copies a selection in Excel
The user responds to your program's prompt (to let your program know the clipboard is ready)
Your program issues the above snippet to grab the clipboard contents into result
Your program processes result as desired
No doubt there are more sophisticated ways, but that should be enough to get you going.
some_string = '''014300071432Gre
014300054037Col
014300065692ASC'''
some_string = """014300071432Gre
014300054037Col
014300065692ASC"""
triple quotes are a multiline string,
you could write this as:
some_string = "014300071432Gre\n014300054037Col\n014300065692ASC"
What is the idomatic/canoncial/best way to get a sibling file (name) in Python 2.7?
That is, if there is file like 'C:\\path\\file.bin' or '/path/file.bin',
how to get 'C:\\path\\anothername.anotherext' or '/path/anothername.anotherext'.
String manipulation, searching for last path separator and replacing the part after that, would of course work, but this seems awfully crude.
In other words, what is idiomatic Python equivalent(s) of these Java snippets:
File sibling = new File(file.getParent(), siblingName);
Or a bit longer for pathname strings:
String siblingPathName = new File(new File(filePathName).getParent(), siblingName).toString();
Note: Java being used above is not relevant to the question.
Note2: If Python 3 has a new way, it would be nice to know that too, even though I'm using Python 2.7 atm.
Use os.path.dirname to get the directory of the file, and then os.path.join to add the sibling's name.
os.path.join(os.path.dirname(f), siblingname)
Here is a problem that has been bothering me a lot about Python, I could really use some help on this:
I'm trying to read some string from a file. The files are .rc suffix where one kind of localized strings are inside for each file. Based on different languages, I used different codepage to decode. Here I only take French file as an example and its code page is 1252. Sadly, every time if there is a double quotes, when I print the string in Python shell, there will be two double quotes unexpectedly.
The line in .rc source file:
La fonction "Test de télécopie" vérifie.
The output string in Python shell:
La fonction ""Test de télécopie"" vérifie.
Some activities I did:
f = open(filename,"r") #Used to open .rc source file
for strline in f.readlines(): #Used to read file line by line
print strline #Used to print in Python shell
Additional Info:
a. The double quotes Hex code in .rc source string is:
b. If I open the .rc source file with web browser, it also displays two double quotes unexpectedly.
c. The .rc source file is confidential, so I didn't attach here.
d. OS: Enu Win7 x64\Python: v2.7
I'm a newbie for Python. Any ideas will be really really appreciated.
Best Regards,
;)
All the misunderstanding comes from that I'm not familiar with .rc files (I never used C++ before) and how does the developer handle the strings. Don't punch me if my answer looks so untutored. :)
After talking with the relevant developers, it's confirmed to be a mechanism added by the .rc file creators which is used to handle the double quotes in strings.
Like a string below:
GUI expects-How are you, "Mark"?
In .rc(or web browser)-"How are you, ""Mark"""?
The .rc file creators add this mechanism of adding one more double quotes to surround the original ones in strings, to ensure once the string is called to display in GUI, it will not be recognized as:
-"How are you,"Mark"?" ==>"How are you," Mark "?"==>This would be a messy double quotes match which the GUI can not display correctly.
So I added a filter to remove this additional double quotes in order to get what I want. And I believe it's easy for the Python users.
It's not a professional answer, but I just hope to let the people who encountered same problem think from a different way.
I really appreciate to everybody that helped me identify the problem above before.
Your console are using another code page other than 1252.
EDIT:
#!/usr/bin/python
# -*- coding: utf-8 -*-
str=u"""La fonction "Test de télécopie" vérifie."""
print str
flat=str.encode("cp1252")
convts=unicode(flat, encoding="cp850") #Change the testing code page here.
print convts