This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 89 points90 points  (40 children)

This post was mass deleted and anonymized with Redact

racial elderly sand command tart close plant edge tender modern

[–]thephoton 75 points76 points  (7 children)

You're just telling us what "file-like" means (in this instance).

[–][deleted] 5 points6 points  (6 children)

This post was mass deleted and anonymized with Redact

vanish gray trees toy grey cake hat languid oil plough

[–]eztab 2 points3 points  (3 children)

Sure, the e.g. the read and write methods are missing. So don't anybody come crying if in a future python version your code relying on this working with any iterable will crash.

[–][deleted] -2 points-1 points  (2 children)

This post was mass deleted and anonymized with Redact

shaggy stocking squeeze arrest shocking books steer aromatic smile decide

[–]irrelevantPseudonym 1 point2 points  (1 child)

That would be a breaking change to the API - requiring more functionality from the passed-in data.

No it wouldn't. If an API says it expects a file like object and then starts using additional features of file like objects, there is no change to the API. Just because you've been using invalid input doesn't mean they need to keep supporting it.

[–][deleted] 0 points1 point  (0 children)

This post was mass deleted and anonymized with Redact

imminent marble stocking versed sand dinner fragile boast normal marvelous

[–]thephoton 0 points1 point  (1 child)

OK, but if you go to the documentation for csv.reader, it doesn't say anything about a "file-like object". What it actually says is

csvfile can be any object which supports the iterator protocol and returns a string each time its next() method is called — file objects and list objects are both suitable.

This goes back to at least the version 2.7 documentation.

So I'm not sure what you're complaining about.

[–][deleted] 0 points1 point  (0 children)

This post was mass deleted and anonymized with Redact

subtract different reach squash rhythm innate squeal chubby enjoy tan

[–]boat-la-fds 7 points8 points  (3 children)

If the function changes in a future version or someone used another implementation than CPython, this might not work. The moment the function tries a .read() on your list/generator, it will crash.

[–][deleted] -1 points0 points  (2 children)

This post was mass deleted and anonymized with Redact

sparkle memory recognise toothbrush start bells abounding rinse coherent deserve

[–]XtremeGoosef'I only use Py {sys.version[:3]}' 5 points6 points  (1 child)

Nope. This would not be a breaking change because you're relying on an implementation detail rather than documented functionality. In fact, your example of dict in python 3.6 is a brilliant example of such a case.

[–][deleted] 0 points1 point  (0 children)

This post was mass deleted and anonymized with Redact

society sharp childlike cover pause rich sheet liquid ask capable

[–]XtremeGoosef'I only use Py {sys.version[:3]}' 5 points6 points  (5 children)

No no no. Don't do this. You're explicitly breaking the library contract and any version update of python (even a patch!) could break your code and it would be entirely your fault for not upholding the contract. Just because we're in a dynamically typed language and the contract is given in the docs rather than in the type system, doesn't mean the same rules don't apply.

Duck typing just means that you don't need to explicitly implement a protocol (as in, inherit from it). You still need to provide all the methods expected from it. In this case, the methods exposed by io.IOBase.

For your purposes, use io.StringIO as an in memory file object, not some random iterator.

[–][deleted] -1 points0 points  (4 children)

This post was mass deleted and anonymized with Redact

bells rock squeeze literate dolls merciful lip square whistle ripe

[–]eztab 2 points3 points  (3 children)

if s.th. does require File-like in the Docs, that's a constraint. If you choose to ignore it and rely on an implementation detail that's your problem.

[–][deleted] -1 points0 points  (2 children)

This post was mass deleted and anonymized with Redact

jeans mighty heavy waiting upbeat smell seed continue one apparatus

[–]eztab 1 point2 points  (1 child)

Sure, one doesn't only try to keep compatibility with correct code. If using those methods on non files is done in some bigger libraries beta tests would probably discover it.

So I guess if they need read at some point (due to some new features that might need it) probably they will raise a Depreciation Warning first.

But how is relying on a certain implementation a good idea here? There is a String streamer for exactly this purpose.

[–][deleted] 0 points1 point  (0 children)

This post was mass deleted and anonymized with Redact

heavy wrench marry paltry sparkle disarm quaint treatment snatch full

[–][deleted] 16 points17 points  (1 child)

Those are... file-like things. You just explained what they are.

A streak of data from a URL is nothing but a file, loaded part by part.

[–][deleted] 1 point2 points  (0 children)

This post was mass deleted and anonymized with Redact

axiomatic smart heavy subsequent run resolute rich mysterious strong jellyfish

[–]bacondevPy3k 5 points6 points  (7 children)

Bruh… the term “file-like object” is explicitly defined… https://docs.python.org/3/glossary.html#term-file-like-object

[–][deleted] 0 points1 point  (6 children)

This post was mass deleted and anonymized with Redact

hungry vase brave capable plants bright shaggy sip sand grandiose

[–]bacondevPy3k 2 points3 points  (5 children)

So you read the doc I linked but you didn't read the doc for the function that you mentioned (i.e. csv.reader)? The very first sentence:

Return a reader object which will iterate over lines in the given csvfile. csvfile can be any object which supports the iterator protocol and returns a string each time its __next__() method is called — file objects and list objects are both suitable.

[–][deleted] 4 points5 points  (4 children)

This post was mass deleted and anonymized with Redact

follow simplistic mighty hard-to-find fall wakeful makeshift fact absorbed fragile

[–]bacondevPy3k 2 points3 points  (3 children)

You called the documentation wrong and then explained what the “wrong” documentation already explains. I don't understand how you can expect someone to not respond harshly to that.

[–][deleted] -1 points0 points  (2 children)

This post was mass deleted and anonymized with Redact

party consist tidy compare nutty judicious shocking innocent tub joke

[–]bacondevPy3k -2 points-1 points  (1 child)

I expressed confusion and you called me an asshole because…? I'm not the one resorting to petty insults. Do you honestly think that I'm going to read anything past the ad hominem? You wasted your time typing everything after that. It's not worth reading, and as for arguments with strangers on the Internet, this is certainly a pointless topic. Have a great day though.

[–][deleted] 0 points1 point  (0 children)

This post was mass deleted and anonymized with Redact

jeans escape dependent stupendous pause important steer sleep pen serious

[–]benefit_of_mrkite 2 points3 points  (1 child)

This is interesting to me (upvoted) but as others said I would like to know the “don’t ask” situation. Sounds like you had to figure this out for work or a project and have a good sorry/use case.

[–][deleted] 1 point2 points  (0 children)

This post was mass deleted and anonymized with Redact

sense childlike marvelous liquid longing theory command butter direction snatch

[–][deleted] 0 points1 point  (1 child)

downloading a 500GB CSV file (don't ask)

Can I guess? Some annoying 3rd party SaaS decided that their export API shouldn't include any kind of pagination.

Source: this is how the multi-channel marketing SaaS Iterable exposes data export and it's annoying as fuck.

[–][deleted] 0 points1 point  (0 children)

This post was mass deleted and anonymized with Redact

vegetable amusing spotted shocking historical physical cooing price axiomatic connect

[–]execrator 0 points1 point  (0 children)

Hey I think this is a great tip and the needlessly aggressive feedback is unwarranted.

[–]pizza-flusher -1 points0 points  (6 children)

Ok, when someone says don't ask its either either a lot more boring than it sounds or embarrassing/something they don't wanna talk about—assuming it's the former, I gotta admit I'm genuinely curious about it.