workspace
stringclasses
1 value
channel
stringclasses
1 value
sentences
stringlengths
1
3.93k
ts
stringlengths
26
26
user
stringlengths
2
11
sentence_id
stringlengths
44
53
timestamp
float64
1.5B
1.56B
__index_level_0__
int64
0
106k
pythondev
help
Hi, I’ve been having some issues with class attributes and properties (python 3). I know how getters and setters are generally used and that it’s the pythonic way to use the `@property` to achieve define getters and setters when they are required. Any feedback is appreciated. Sorry for the longish quesiton. Now my question is twofold: Part 1 How and why should I define the attribute: ``` class MyClass: def __init__(self, name): self.name = name # or instead self._name = name # which is preferred and why? @property def name(self): return self._name ``` Part 2 With the `@property` decorator, I can add some logic to setting an attribute after the fact. So if it has been publicly set like: `instance.attribute = some_value`, I can use a property to route this through a setter and check the if, for example, `some_value` is in a specific range. This makes sense to me. However, what if want to use a setter as defined above, but indicate to the user that it is not part of the public API of the class? For example, if I don’t want client code to use a specific attribute, I’d write `self._attribute`. How would I do this with property decorator?
2019-03-04T14:26:28.466100
Dominga
pythondev_help_Dominga_2019-03-04T14:26:28.466100
1,551,709,588.4661
11,921
pythondev
help
Why do you want to use a setter but hide it?
2019-03-04T14:28:30.467200
Lillia
pythondev_help_Lillia_2019-03-04T14:28:30.467200
1,551,709,710.4672
11,922
pythondev
help
so, first of all the `_` is convention, nothing _stops_ client code from using it. That aside, you can use the property assignment instead of the decorator
2019-03-04T14:28:40.467300
Clemmie
pythondev_help_Clemmie_2019-03-04T14:28:40.467300
1,551,709,720.4673
11,923
pythondev
help
This is what you would do - it is not an accepted answer, but it is correct <https://stackoverflow.com/questions/42137210/python-property-with-public-getter-and-private-setter>
2019-03-04T14:29:07.468000
Clemmie
pythondev_help_Clemmie_2019-03-04T14:29:07.468000
1,551,709,747.468
11,924
pythondev
help
thank you <@Clemmie>, I’ll have a look at that link <@Lillia> I thought that it could be used to validate the data for the attribute when set internally by my class. Would such a validation be unnecessary if the attribute is set internally by the class?
2019-03-04T14:35:17.470400
Dominga
pythondev_help_Dominga_2019-03-04T14:35:17.470400
1,551,710,117.4704
11,925
pythondev
help
Can't you do the validation in the method that's setting the new value?
2019-03-04T14:35:36.470700
Lillia
pythondev_help_Lillia_2019-03-04T14:35:36.470700
1,551,710,136.4707
11,926
pythondev
help
Getters/setters make the life of the caller easier, that's really it.
2019-03-04T14:35:54.471100
Lillia
pythondev_help_Lillia_2019-03-04T14:35:54.471100
1,551,710,154.4711
11,927
pythondev
help
Makes for less typing.
2019-03-04T14:36:07.471500
Lillia
pythondev_help_Lillia_2019-03-04T14:36:07.471500
1,551,710,167.4715
11,928
pythondev
help
I wouldn't add unnecessary boilerplate if you're already setting the value yourself.
2019-03-04T14:36:26.471800
Lillia
pythondev_help_Lillia_2019-03-04T14:36:26.471800
1,551,710,186.4718
11,929
pythondev
help
(I'll add a caveat, a setter _may_ be good if you use this validation in many places)
2019-03-04T14:36:52.472500
Lillia
pythondev_help_Lillia_2019-03-04T14:36:52.472500
1,551,710,212.4725
11,930
pythondev
help
Actually I take that back; using a setter for validation is a bit weird.
2019-03-04T14:40:01.476000
Lillia
pythondev_help_Lillia_2019-03-04T14:40:01.476000
1,551,710,401.476
11,931
pythondev
help
Validation on attribute change is a hard one. The client can still access the underlying attribute without calling the setter. It is kind of like front-end back-end validation. Good to do on the frontend for speed/clarity, but if you don’t check on the backend also you will compromised
2019-03-04T14:40:32.477100
Clemmie
pythondev_help_Clemmie_2019-03-04T14:40:32.477100
1,551,710,432.4771
11,932
pythondev
help
So, by all means validate on change, but you should also when the value is _used_
2019-03-04T14:40:53.477900
Clemmie
pythondev_help_Clemmie_2019-03-04T14:40:53.477900
1,551,710,453.4779
11,933
pythondev
help
Hm, I thought it is more readable and contained in that class. I have an instance of an Account class, modeling a bank account. For that bank account, I have a balance attribute and a corresponding setter, that checks if it ever drops below 0. If it does, it raises a ValueError. I also have an Atm class which uses the account object and calls `account.withdraw(amount)` or `account.deposit(amount)`. To me it seemed useful to have that ‘data integrity’ check inside the setter of the balance.
2019-03-04T14:41:01.478200
Dominga
pythondev_help_Dominga_2019-03-04T14:41:01.478200
1,551,710,461.4782
11,934
pythondev
help
I would have `withdraw()` do the check. Adding a setter simply for validation is unneeded boilerplate. It doesn't make the life of the caller any easier.
2019-03-04T14:41:49.479000
Lillia
pythondev_help_Lillia_2019-03-04T14:41:49.479000
1,551,710,509.479
11,935
pythondev
help
So what is a setter for if not for validation of the to be set value?
2019-03-04T14:43:25.481600
Dominga
pythondev_help_Dominga_2019-03-04T14:43:25.481600
1,551,710,605.4816
11,936
pythondev
help
<https://pythondev.slack.com/archives/C07EFMZ1N/p1551728154471100>
2019-03-04T14:43:40.482000
Lillia
pythondev_help_Lillia_2019-03-04T14:43:40.482000
1,551,710,620.482
11,937
pythondev
help
For example, following your account example, you may want to do validation when a caller sets the actual account balance.
2019-03-04T14:44:03.482900
Lillia
pythondev_help_Lillia_2019-03-04T14:44:03.482900
1,551,710,643.4829
11,938
pythondev
help
Rather than having `account.setBalance(x)`, you can define a setter and simply do `account.balance = x`
2019-03-04T14:44:23.483700
Lillia
pythondev_help_Lillia_2019-03-04T14:44:23.483700
1,551,710,663.4837
11,939
pythondev
help
Hi guys, I know this is a long shot, but I have 30mn left to deliver a project and no time to do manual edits, or write the script fast enough due to my basic knowledge. I have 100+ json files, each file is stored in individual directory into my working directory. I need to update one value for one object in each file (it is the same object for all files, and needs to be set to the same value). Any chance someone could offer some very quick guidance? *THANKS*
2019-03-04T14:44:27.484000
Ludie
pythondev_help_Ludie_2019-03-04T14:44:27.484000
1,551,710,667.484
11,940
pythondev
help
Your setter can do the same validation that `setBalance` would have done, but it makes the syntax cleaner for the caller. Does that make sense?
2019-03-04T14:44:53.484500
Lillia
pythondev_help_Lillia_2019-03-04T14:44:53.484500
1,551,710,693.4845
11,941
pythondev
help
You aren't expecting the caller to directly set the balance, correct?
2019-03-04T14:45:16.485100
Lillia
pythondev_help_Lillia_2019-03-04T14:45:16.485100
1,551,710,716.4851
11,942
pythondev
help
linux or windows?
2019-03-04T14:45:49.485300
Clemmie
pythondev_help_Clemmie_2019-03-04T14:45:49.485300
1,551,710,749.4853
11,943
pythondev
help
macOS :smile:
2019-03-04T14:45:55.485700
Ludie
pythondev_help_Ludie_2019-03-04T14:45:55.485700
1,551,710,755.4857
11,944
pythondev
help
ok, great, you have a terminal
2019-03-04T14:46:04.486000
Clemmie
pythondev_help_Clemmie_2019-03-04T14:46:04.486000
1,551,710,764.486
11,945
pythondev
help
yes, i know python basics and for loops
2019-03-04T14:46:29.486700
Ludie
pythondev_help_Ludie_2019-03-04T14:46:29.486700
1,551,710,789.4867
11,946
pythondev
help
but not enough about json specifically
2019-03-04T14:46:33.487000
Ludie
pythondev_help_Ludie_2019-03-04T14:46:33.487000
1,551,710,793.487
11,947
pythondev
help
what is the value you need to change, and is it part of a string that is unique in each file?
2019-03-04T14:46:33.487100
Clemmie
pythondev_help_Clemmie_2019-03-04T14:46:33.487100
1,551,710,793.4871
11,948
pythondev
help
That makes sense. That is correct. The caller would invoke the `withdraw` method of the account object, which would set the balance internally.
2019-03-04T14:46:34.487400
Dominga
pythondev_help_Dominga_2019-03-04T14:46:34.487400
1,551,710,794.4874
11,949
pythondev
help
Right; you already have methods that the caller will use. Those methods can do the validation just fine.
2019-03-04T14:46:54.488100
Lillia
pythondev_help_Lillia_2019-03-04T14:46:54.488100
1,551,710,814.4881
11,950
pythondev
help
Ahhh, so if my Atm class would set the balance directly, then it would make sense to create a setter?
2019-03-04T14:46:58.488400
Dominga
pythondev_help_Dominga_2019-03-04T14:46:58.488400
1,551,710,818.4884
11,951
pythondev
help
Correct!
2019-03-04T14:47:02.488600
Lillia
pythondev_help_Lillia_2019-03-04T14:47:02.488600
1,551,710,822.4886
11,952
pythondev
help
Sample file: ```{ "version": "1.0", "identifier": "2854-1269-8-1", "title": "Welcome To The World Of More", "language": "en" }```
2019-03-04T14:47:05.488800
Ludie
pythondev_help_Ludie_2019-03-04T14:47:05.488800
1,551,710,825.4888
11,953
pythondev
help
And do you need to maintain the directory strucutre
2019-03-04T14:47:10.489100
Clemmie
pythondev_help_Clemmie_2019-03-04T14:47:10.489100
1,551,710,830.4891
11,954
pythondev
help
Thank you very much.
2019-03-04T14:47:13.489400
Dominga
pythondev_help_Dominga_2019-03-04T14:47:13.489400
1,551,710,833.4894
11,955
pythondev
help
i need to change the language value to `fr`
2019-03-04T14:47:14.489500
Ludie
pythondev_help_Ludie_2019-03-04T14:47:14.489500
1,551,710,834.4895
11,956
pythondev
help
Yes, i need to keep that structure
2019-03-04T14:47:26.489800
Ludie
pythondev_help_Ludie_2019-03-04T14:47:26.489800
1,551,710,846.4898
11,957
pythondev
help
ok, give me a couple of minutes
2019-03-04T14:47:34.490000
Clemmie
pythondev_help_Clemmie_2019-03-04T14:47:34.490000
1,551,710,854.49
11,958
pythondev
help
Thanks!!
2019-03-04T14:47:44.490200
Ludie
pythondev_help_Ludie_2019-03-04T14:47:44.490200
1,551,710,864.4902
11,959
pythondev
help
saving my ass
2019-03-04T14:47:46.490500
Ludie
pythondev_help_Ludie_2019-03-04T14:47:46.490500
1,551,710,866.4905
11,960
pythondev
help
<@Lillia> and <@Clemmie> thanks for your time and the explanations. I appreciate it.
2019-03-04T14:48:20.491100
Dominga
pythondev_help_Dominga_2019-03-04T14:48:20.491100
1,551,710,900.4911
11,961
pythondev
help
Welcome!
2019-03-04T14:48:34.491300
Lillia
pythondev_help_Lillia_2019-03-04T14:48:34.491300
1,551,710,914.4913
11,962
pythondev
help
Good questions
2019-03-04T14:48:46.491500
Lillia
pythondev_help_Lillia_2019-03-04T14:48:46.491500
1,551,710,926.4915
11,963
pythondev
help
Heh heh, this is like a coding challenge competition.
2019-03-04T14:49:45.491700
Sasha
pythondev_help_Sasha_2019-03-04T14:49:45.491700
1,551,710,985.4917
11,964
pythondev
help
is there a single file in each directory, and nothing else?
2019-03-04T14:50:53.491900
Clemmie
pythondev_help_Clemmie_2019-03-04T14:50:53.491900
1,551,711,053.4919
11,965
pythondev
help
yes
2019-03-04T14:50:57.492100
Ludie
pythondev_help_Ludie_2019-03-04T14:50:57.492100
1,551,711,057.4921
11,966
pythondev
help
ok, first, make a whole copy of your directory structure in case we screw up
2019-03-04T14:51:18.492300
Clemmie
pythondev_help_Clemmie_2019-03-04T14:51:18.492300
1,551,711,078.4923
11,967
pythondev
help
`/dir/file1.json` `/dir/file2.json` `/dir/file3.json`
2019-03-04T14:51:23.492500
Ludie
pythondev_help_Ludie_2019-03-04T14:51:23.492500
1,551,711,083.4925
11,968
pythondev
help
ok
2019-03-04T14:51:30.492700
Ludie
pythondev_help_Ludie_2019-03-04T14:51:30.492700
1,551,711,090.4927
11,969
pythondev
help
wait
2019-03-04T14:52:24.493000
Ludie
pythondev_help_Ludie_2019-03-04T14:52:24.493000
1,551,711,144.493
11,970
pythondev
help
the directory structure above is innacurate, one second
2019-03-04T14:52:32.493200
Ludie
pythondev_help_Ludie_2019-03-04T14:52:32.493200
1,551,711,152.4932
11,971
pythondev
help
then in a terminal that is at the root of the directory structure you will want to run `sed -i 's/"language": "en"/"language": "fr"/g" */*.json`
2019-03-04T14:52:32.493300
Clemmie
pythondev_help_Clemmie_2019-03-04T14:52:32.493300
1,551,711,152.4933
11,972
pythondev
help
`sed` !
2019-03-04T14:52:39.493600
Lillia
pythondev_help_Lillia_2019-03-04T14:52:39.493600
1,551,711,159.4936
11,973
pythondev
help
`/dir/a1/file.json` `/dir/a2/file.json` `/dir/a3/file.json`
2019-03-04T14:52:52.493900
Ludie
pythondev_help_Ludie_2019-03-04T14:52:52.493900
1,551,711,172.4939
11,974
pythondev
help
that says “use the stream editor in place to replace string 1 with sdtring 2, globally in each file
2019-03-04T14:53:07.494200
Clemmie
pythondev_help_Clemmie_2019-03-04T14:53:07.494200
1,551,711,187.4942
11,975
pythondev
help
ok, so first i cd to my test directory
2019-03-04T14:53:34.494400
Ludie
pythondev_help_Ludie_2019-03-04T14:53:34.494400
1,551,711,214.4944
11,976
pythondev
help
yup
2019-03-04T14:53:43.494600
Clemmie
pythondev_help_Clemmie_2019-03-04T14:53:43.494600
1,551,711,223.4946
11,977
pythondev
help
Watch out for the quote after `/g`.
2019-03-04T14:53:59.494800
Sasha
pythondev_help_Sasha_2019-03-04T14:53:59.494800
1,551,711,239.4948
11,978
pythondev
help
yup, my bad - should be a single `'`
2019-03-04T14:54:19.495000
Clemmie
pythondev_help_Clemmie_2019-03-04T14:54:19.495000
1,551,711,259.495
11,979
pythondev
help
its not meant to be there?
2019-03-04T14:54:20.495200
Ludie
pythondev_help_Ludie_2019-03-04T14:54:20.495200
1,551,711,260.4952
11,980
pythondev
help
oki
2019-03-04T14:54:25.495400
Ludie
pythondev_help_Ludie_2019-03-04T14:54:25.495400
1,551,711,265.4954
11,981
pythondev
help
`sed -i 's/"language": "en"/"language": "fr"/g' */*.json`
2019-03-04T14:54:36.495600
Clemmie
pythondev_help_Clemmie_2019-03-04T14:54:36.495600
1,551,711,276.4956
11,982
pythondev
help
`sed -i 's/"language": "en"/"language": "fr"/g' */*.json`
2019-03-04T14:54:38.495800
Ludie
pythondev_help_Ludie_2019-03-04T14:54:38.495800
1,551,711,278.4958
11,983
pythondev
help
this is why we copied first
2019-03-04T14:54:45.496000
Clemmie
pythondev_help_Clemmie_2019-03-04T14:54:45.496000
1,551,711,285.496
11,984
pythondev
help
`sed: 1: "1/article.json": invalid command code /`
2019-03-04T14:55:05.496200
Ludie
pythondev_help_Ludie_2019-03-04T14:55:05.496200
1,551,711,305.4962
11,985
pythondev
help
hopefully yes. we may need to do some string escaping to make it work for sure
2019-03-04T14:55:07.496400
Clemmie
pythondev_help_Clemmie_2019-03-04T14:55:07.496400
1,551,711,307.4964
11,986
pythondev
help
I think this is connected to my directory structure
2019-03-04T14:55:13.496600
Ludie
pythondev_help_Ludie_2019-03-04T14:55:13.496600
1,551,711,313.4966
11,987
pythondev
help
the error
2019-03-04T14:55:16.496800
Ludie
pythondev_help_Ludie_2019-03-04T14:55:16.496800
1,551,711,316.4968
11,988
pythondev
help
nope on os x I forgot -e
2019-03-04T14:55:28.497000
Clemmie
pythondev_help_Clemmie_2019-03-04T14:55:28.497000
1,551,711,328.497
11,989
pythondev
help
`sed -i -e 's/"language": "en"/"language": "fr"/g' */*.json`
2019-03-04T14:55:46.497200
Clemmie
pythondev_help_Clemmie_2019-03-04T14:55:46.497200
1,551,711,346.4972
11,990
pythondev
help
ok, it ran
2019-03-04T14:55:57.497400
Ludie
pythondev_help_Ludie_2019-03-04T14:55:57.497400
1,551,711,357.4974
11,991
pythondev
help
let me check the output :smile:
2019-03-04T14:56:00.497600
Ludie
pythondev_help_Ludie_2019-03-04T14:56:00.497600
1,551,711,360.4976
11,992
pythondev
help
or maybe not, one second
2019-03-04T14:56:07.497800
Clemmie
pythondev_help_Clemmie_2019-03-04T14:56:07.497800
1,551,711,367.4978
11,993
pythondev
help
it worked!!
2019-03-04T14:56:25.498000
Ludie
pythondev_help_Ludie_2019-03-04T14:56:25.498000
1,551,711,385.498
11,994
pythondev
help
but
2019-03-04T14:56:27.498200
Ludie
pythondev_help_Ludie_2019-03-04T14:56:27.498200
1,551,711,387.4982
11,995
pythondev
help
it created a duplicate file too, with `file.json-e`
2019-03-04T14:56:48.498400
Ludie
pythondev_help_Ludie_2019-03-04T14:56:48.498400
1,551,711,408.4984
11,996
pythondev
help
I thought it might
2019-03-04T14:57:07.498600
Clemmie
pythondev_help_Clemmie_2019-03-04T14:57:07.498600
1,551,711,427.4986
11,997
pythondev
help
does the txt look correct in those?
2019-03-04T14:57:14.498800
Clemmie
pythondev_help_Clemmie_2019-03-04T14:57:14.498800
1,551,711,434.4988
11,998
pythondev
help
no
2019-03-04T14:57:34.499000
Ludie
pythondev_help_Ludie_2019-03-04T14:57:34.499000
1,551,711,454.499
11,999
pythondev
help
the -e files seem to have the old data
2019-03-04T14:57:40.499200
Ludie
pythondev_help_Ludie_2019-03-04T14:57:40.499200
1,551,711,460.4992
12,000
pythondev
help
and the files with the proper name, have the proper data
2019-03-04T14:57:49.499400
Ludie
pythondev_help_Ludie_2019-03-04T14:57:49.499400
1,551,711,469.4994
12,001
pythondev
help
is there a way to not generate the second file?
2019-03-04T14:58:07.499600
Ludie
pythondev_help_Ludie_2019-03-04T14:58:07.499600
1,551,711,487.4996
12,002
pythondev
help
ok, different try
2019-03-04T14:58:21.499800
Clemmie
pythondev_help_Clemmie_2019-03-04T14:58:21.499800
1,551,711,501.4998
12,003
pythondev
help
I think yes, try this
2019-03-04T14:58:34.500000
Clemmie
pythondev_help_Clemmie_2019-03-04T14:58:34.500000
1,551,711,514.5
12,004
pythondev
help
`sed -i'' 's/"language": "en"/"language": "fr"/g' */*.json`
2019-03-04T14:59:08.500200
Clemmie
pythondev_help_Clemmie_2019-03-04T14:59:08.500200
1,551,711,548.5002
12,005
pythondev
help
ok
2019-03-04T14:59:19.500400
Ludie
pythondev_help_Ludie_2019-03-04T14:59:19.500400
1,551,711,559.5004
12,006
pythondev
help
Error: `sed: 1: "1/article.json": invalid command code /`
2019-03-04T14:59:31.500600
Ludie
pythondev_help_Ludie_2019-03-04T14:59:31.500600
1,551,711,571.5006
12,007
pythondev
help
we’ll get it
2019-03-04T14:59:43.500800
Clemmie
pythondev_help_Clemmie_2019-03-04T14:59:43.500800
1,551,711,583.5008
12,008
pythondev
help
how much time do you have?
2019-03-04T14:59:48.501000
Clemmie
pythondev_help_Clemmie_2019-03-04T14:59:48.501000
1,551,711,588.501
12,009
pythondev
help
15mn before i get in trouble, more or less
2019-03-04T14:59:56.501200
Ludie
pythondev_help_Ludie_2019-03-04T14:59:56.501200
1,551,711,596.5012
12,010
pythondev
help
May be easier to just mass-delete the `-e` files?
2019-03-04T14:59:58.501400
Sasha
pythondev_help_Sasha_2019-03-04T14:59:58.501400
1,551,711,598.5014
12,011
pythondev
help
ok
2019-03-04T14:59:59.501600
Clemmie
pythondev_help_Clemmie_2019-03-04T14:59:59.501600
1,551,711,599.5016
12,012
pythondev
help
going to the bathroom for 1mn
2019-03-04T15:01:52.501900
Ludie
pythondev_help_Ludie_2019-03-04T15:01:52.501900
1,551,711,712.5019
12,013
pythondev
help
<@Lillia> <@Jettie> that worked thanks a bunch. just didn't expect the format to be in bytes so I will try with mode='rt'
2019-03-04T15:02:46.502800
Alvina
pythondev_help_Alvina_2019-03-04T15:02:46.502800
1,551,711,766.5028
12,014
pythondev
help
Am back!
2019-03-04T15:04:33.503000
Ludie
pythondev_help_Ludie_2019-03-04T15:04:33.503000
1,551,711,873.503
12,015
pythondev
help
working on it
2019-03-04T15:06:35.503200
Clemmie
pythondev_help_Clemmie_2019-03-04T15:06:35.503200
1,551,711,995.5032
12,016
pythondev
help
`find ~/tmp/a -type f -exec sed -i '' 's/"language": "en"/"language": "fr"/g' {} \;` make sure to replace `/tmp/a` with the path to your current directory
2019-03-04T15:07:55.503400
Clemmie
pythondev_help_Clemmie_2019-03-04T15:07:55.503400
1,551,712,075.5034
12,017
pythondev
help
yay, all set.
2019-03-04T15:08:31.503800
Alvina
pythondev_help_Alvina_2019-03-04T15:08:31.503800
1,551,712,111.5038
12,018
pythondev
help
wait
2019-03-04T15:09:34.504000
Ludie
pythondev_help_Ludie_2019-03-04T15:09:34.504000
1,551,712,174.504
12,019
pythondev
help
your command worked , i think you missed a space
2019-03-04T15:09:43.504200
Ludie
pythondev_help_Ludie_2019-03-04T15:09:43.504200
1,551,712,183.5042
12,020