Python switch between users [duplicate] - python

This question already has answers here:
Is there a portable way to get the current username in Python?
(15 answers)
Closed 2 years ago.
I am trying to implement a switch at the beginning of my code, in order to change the working directory based on who's running the code. In Stata, this exists and looks like the following:
if "`c(username)'" == "albert" {
local PATH "/home/albert/Dropbox/Project1"
}
else if "`c(username)'" == "charlene" {
local PATH "C:/Users/charlene/Documents/Dropbox/Project1"
}
I am just missing the "user" part in python. Does anyone knows if that even exists ?

Would the getpass.getuser() function do the trick?

Related

How can I access dict value in Flask config file for using in Jquery? [duplicate]

This question already has answers here:
How to access flask config in javascript?
(2 answers)
JavaScript raises SyntaxError with data rendered in Jinja template
(3 answers)
Closed 8 months ago.
I want to show the value when the key is mapping in Jquery.
I typed ACTION_STATES value in config.py Flask as dict type.
[Saved in DB]
[config.py]
ACTION_STATES = {
'Active': 'Go to the Next step',
'Completed': 'All process is finished'
}
I tried to use config value like below. Then the result is not what I want to do.
[Accessing in Jquery]
action_states = '{{config.ACTION_STATES}}'
console.log(action_states)
action_states = '{{config.ACTION_STATES.items()}}'
console.log(action_states)
How can I get the value in config.py using Jquery?
Is it possible to access?

Is there a way to hash text that a user puts into the command line in Python? [duplicate]

This question already has answers here:
Getting a hidden password input
(6 answers)
Closed 1 year ago.
what I am trying to do is a command line sign in. The user puts in their credentials like this:
username = str(input('Username: '))
password = str(input('Password: '))
However, when they put in the password, I want it to be hashed on the command line. So for example, they type in "Password" with their keyboard, and that is how the program recognizes it, but on their screen it is hashed to be ********, so they can't see their own password like on a lot of websites. However, I can't find any way to do this. How would you go about doing this?
Install stdiomask
pip install stdiomask
Then:
import stdiomask
pwd = stdiomask.getpass()
You can use options such as:
stdiomask.getpass(prompt='pwd: ')
stdiomask.getpass(mask='x')

How to check time from internet using bash or python? [duplicate]

This question already has answers here:
ntp client in python
(4 answers)
Python Getting date online?
(9 answers)
Closed 5 years ago.
This should be a very simple task, but after 2 hours of searching and reading documents I failed to find a way to check the date and time from the internet using bash or python without installing anything extra like ntplib.
I am basically looking for an equivalent of pythons now() or bash $ date, but using an NTP (or any other way) to get the correct date and time from the internet. All the methods I find (such as ntpd) are meant to correct the system time, which is not my purpose.
Easy way to get the correct time in python
import time
import os
try:
import ntplib
client = ntplib.NTPClient()
response = client.request('pool.ntp.org')
os.system('date ' + time.strftime('%m%d%H%M%Y.%S',time.localtime(response.tx_time)))
except:
print('Could not sync with time server.')
print('Done.')

Cannot loop over an array [duplicate]

This question already has an answer here:
Access specific information within worklogs in jira-python
(1 answer)
Closed 5 years ago.
I'm trying to insert the following worklog JSON fields that are part of an array using Python however I don't seem to be able to loop over an array in Python.
See code snippet below:
try:
worklogs_to_insert = []
for i in issue.fields.worklog["worklogs"]:
worklogs_to_insert.append(i)
except AttributeError as e:
log.info("Something went wrong when processing worklogs. :(")
log.info(e)
I am getting the following error when script is run:
Something went wrong when processing worklogs. :(
type object 'PropertyHolder' has no attribute 'worklog'
try using
for i in issue.fields.worklog.raw["worklogs"]

Loop through json file and delete key if it matches [duplicate]

This question already has answers here:
Is there a simple way to delete a list element by value?
(25 answers)
Closed 6 years ago.
I have the following structure on a JSON file:
{
"channels": [
"180873781382873088",
"181268808055521280",
"183484852287307777",
"174886257636147201",
"174521530573651968"
]
}
I want to know how I can loop through the file searching for a specific string, and delete it if it matches.
Thank you.
EDIT: A Google search pointed me to using a for loop and using the del command to remove the key, so here's what I tried:
channel = "180873781382873088"
for item in data['channels']:
del channel
But it only deletes the variable channel, not the key that matches it's value.
Try
data['channels'].remove(channel)
instead of the for loop.
This will automatically search the array and remove any key matching your variable. If you need help saving the results to a file I would open another question.

Categories

Resources