Serial Write Vs Print

0 views
Skip to first unread message

Bertoldo Beyer

unread,
Aug 4, 2024, 6:58:47 PM8/4/24
to hostnalobo
Isthere a difference between the two? Or is any one more Pythonic? I'm trying to write a bunch of HTML to file so I need a bunch of write/print statements through my file(but I don't need a templating engine).

print and file.write are quite different operations. file.write just directly writes a string to a file. print is more like "render values to stdout as text". Naturally, the result of rendering a string as text is just the string, so print >> f, my_string and f.write(my_string) are nearly interchangeable (except for the addition of a newline). But your choice between file.write and print should normally be based on what you're doing; are you writing a string to a file, or are you rendering values to a file?


Sure, print is not strictly necessary, in that you can implement it with file.write. But then file.write is not strictly necessary, because you can implement it with the operations in os for dealing with file descriptors. Really they're operations on different levels, and you should use whichever is more most appropriate for your use (normally the level other nearby code is working on, or the highest level that doesn't get in your way).


I do feel that the print >> f syntax is fairly horrible, and is a really good example of why print should have been a function all along. This is much improved in Python 3. But even if you're writing Python 2 code that you're planning to port to Python 3, it is much easier to convert print >> f, thing1, thing2, thing3, ... to print(thing1, thing2, thing3, file=f) than it is to convert the circumlocution where you roll your own code to do the equivalent of print's rendering and then call f.write(text). I'm pretty sure the semi-automatic converter from Python 2 to Python 3 will even do the conversion for you, which it couldn't possibly do if you avoid the print >> f form.


Note that this changes print in other ways, most obviously in that you need to add brackets around its arguments. But the changes are all improvments, which is the whole reason for the change in Python 3. While you're at it, consider using all the future statements to get as many backported improvements from Python 3 as possible.


print, by default, prints to standard output, which in fact is a "file-like" object (sys.stdout). The standard output itself has a write() method. Using print >> f seems to be an unnecessary abstraction.


The ">>" idiom for printing was borrowed from C++ in Python's early days, and is rather unpythonic - so much that it no longer exists in Python 3.x - where one can use the print, now a function instead of a statement, to write to a file - but with no special syntax for that.


As @agf points in his answer, using "print" to write to a file does more things than simply calling write - it automatically calls str(obj) to get a string representation of the object, whereas .write require that a (byte) string be passed as parameter - in Python world "explicit is better than implicit", so one more motive for one to go with file.write instead.


On python 2 I prefer file.write because the >> syntax is deprecated. For python 3 you might prefer to use the print function instead, which you should note does some extra things (for example automatically convert numbers to strings for you, etc).


Hey everyone I had a doubt that whether we can able to print the line and take the user input in the same line in the fortran programming is it possible if yes can anyone please help me out in this and give a sample example of it


Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.


Thanks Ken. Also I made mistake. I thought the the print function being used in this video was the build in JavaScript print() method. I realized Dave made his own print function, which uses the document.write().


I am confused by this exchange after reading your linked article and searching the MDN documentation. Should we not be using the document.write() method to write html to the page? If not, what is the alternative "built[sic] in JavaScript print() method" mentioned by Ali above? I cannot find any reference to it in the MDN documentation.


From what I understand, document.write() is the standard function to write text to the DOM (i.e. browser). However, Dave is using print() because it calls the function in which document.write() appears. The reason behind this is the DRY methodology. By placing document.write() into a function, now we don't have to repeat the code that appears in that function, we simply call that function by using print(). Hope that makes sense.


print* outputs strings in UTF-8 format (hopefully that can changed so that the desired output encoding can be selected), no matter how the string is encoded.

This is important if you have strings with different encodings that you want to print out together.


The general principle is that for print the output stream determines the encoding (UTF-8 by default for all built-in IO types); for write the object itself determines the raw data, including the encoding. Endianness may be an exception to that, but again, I/O types with different endianness can and should be added outside of Base.


Symptoms: After trying to print a "complex" image the printer made too much breaks, and failed.

The problem is, it then didn't came up. I mean, I restarted the printer, cupsd, the whole system, and nothing.

I was unable to print again, it showed "Unable to write print data: Input/output error" and refused to print anything.


There was probably an option in the web interface combobox for doing that. The problem is that I didn't realise it was stopped, so didn't know how to make it work. Moreover, some posts in the web recommended reinstalling the printer (which would skip the problem by creating a new printer).


The rationale for providing print is that displayand write both have specific output conventions, and thoseconventions restrict the ways that an environment can change thebehavior of display and write procedures. No outputconventions should be assumed for print, so that environmentsare free to modify the actual output generated by print inany way.


The println function is not equivalent to println inother languages, because println uses printing conventionsthat are closer to write than to display. For a closeranalog to println in other languages, use displayln.


A port print handler optionally accepts a third argument, whichcorresponds to the optional third argument to print; if aprocedure given to port-print-handler does not accept a thirdargument, it is wrapped with a procedure that discards the optionalthird argument.


A global port print handler optionally accepts a thirdargument, which corresponds to the optional third argument toprint. If a procedure given toglobal-port-print-handler does not accept a third argument,it is wrapped with a procedure that discards the optional thirdargument.


Note: Similar question has been posted years ago. Yet the debugging procudure provided by the long answer didn't help. And the last posted, supposedly the solution for it is very unintuitive. So here I am, post the question again.


The printing system of my computer used to be working fine. Yet today when I try to print a document, the printer do not respond. Downgrading all cup related packages to earliest version I have on my computer doesn't help. Neither does reinstalling the printer driver.


Sometimes a hardware issue is indeed -- simply hardware issue... In my case, it turned out that our school changed the way printing jobs are organized. They blocked the direct printing, maybe through firewall policy. And redirect all printing jobs through a central server with SAMBA protocol. Hence I can ping the office printer, yet I can't send any printing data to the printer. The issue eventually got solved after contacting the IT staff and reconfigured my computer according to their change.


If you met a similar problem, and cannot figure out the source of it on your own end. You should probably contact the IT staff of your company/organization. Maybe they simply changed some settings without informing you.


Then it is clear that you successfully access the printer and yet the printer rejected your access through 9100 port. When this happens, ask your IT staff to see if the policy is changed without telling you and how to configure to use the new printing system.


We had all of the symptoms above after a new network was put into place. After ensuring that port 9100 was open and could connect over telnet, we changed the printer driver from "HP" to "Generic PDF", the broken pipe error went away and were able to print successfully.


When they finally print from Write, the first page is OK but there is also a second page printed that contains a single line of text with graphics characters interspersed with PU3443, 6779RR3 and other similar text.


I've built up a small collection of print sheets for the MK3S now, consisting of 3 Smooth PEI, a Prusa Textured sheet and a third party textured sheet. They're all set up in the settings for quick and easy swaps, but it'd be even easier if I could remember which smooth and which textured sheets were which. lol


I'm going to mark them for quick identification, but it got me wondering... What (if anything) have people used to mark theirs? Paint, sharpie markers, labels, tip-ex or something else entirely?! Just curious. ?


Best tip I've found was from @jbinfl to simply lay down a strip of blue painter's tape. You can label the sheet along with the Live-Z setting. You can use alcohol to wipe away sharpie marks when it changes.


I do like a metallic sharpie! But painters tape seems a simple and very good idea. Keeping an easily editable record of Z height settings for various filament types on the actual sheet being used does seem handy. I write them all down in a notebook presently which is simple enough, but writing values on the print sheet would be easier still. Thanks.

3a8082e126
Reply all
Reply to author
Forward
0 new messages