I have created a job in Jenkins, for which user provides the value of a specific parameter, let's say PYTHON_PARM, as an input. On this job I execute a python script (using Python Plugin). The problem is that I want to use as a variable the user input $PYTHON_PARM parameter. This is not considered as an environment variable, so when trying to use os.environ['PYTHON_PARM'], this doesn't work.
Any idea?
Thanks,
Solution:
from os import environ
Type = environ['Type']
Related
I've been learning Python and decided to make a note-taking utility that runs in bash. I've worked out the basic 'guts' and I want to add a feature that allows a new user to configure their notes (for instance, set the directory where new note files are stored).
I realize this means running a 'install/config' function that is only called the first time the user runs the script (or until they configure it). I don't know what this concept is called, and after some research, cannot find anything about it w/Python.
I'm using argparse. You call the python script from the shell and can optionally use it with arguments. If it would help to see my code, please let me know and I'll format it (it's long and needs to be edited a bit if I want to post). Thanks.
tl;dr How do you run a function only once in Python (either first time code is executed, or until the function's purpose - in this case, setting a file path - is fulfilled)?
I was given a task to do in python, and have to "store an expression to the system variable __return__", then later return the value of __return__ in another function.
I understand what __return__ is, however, I am not sure of how one would "store" something to it for later use?
Should I ask for further clarification on the topic, or is there a way to do this?
A system variable is a global variable that is accessible anywhere from your system.
In Windows, for example, you can see your system variables by pressing Winkey and then searching for Edit the system environment variables, and then clicking on the environment variables... button.
(Note: In Windows you can have separate variables for user and for the whole system).
In Linux, printenv in a terminal will print your environment variables.
Back to Python:
You can store said expression to the relevant system variable by using
import os
os.environ['__return__'] = some_expression
And get the expression back by using
def foo():
return os.environ['__return__']
I am using TeamCity to test my code. I am using command line from TC and I am running a python test suite that tests the code.
I defined an environment variable (named "Server") and gave it a value ("production") and I want to pass it in a way that my python script will be able to access it and save it's value in the code.
I tried looking in TC documentation as well in Python's documentation and I couldn't find it.
I will appreciate your help.
Probably you should use os.environ
assert os.environ['Server'] == 'production'
I am learning python programming and I am just going through easy exercises. One of them has me create a class as follows:
class MyFirstClass:
Pass
That is it. I save this and then when I try to import the file using python3.3 in a Mac Terminal and assign an object:
a = MyFirstClass()
I get a syntax error. Am I not running the program correctly? I have performed this task in IDLE but it does not seem to work when I am using Python in the terminal.
Python is case-sensitive. Pass should be pass.
I'm trying to get results of db.stats() mongo shell command in my python code (for monitoring purposes).
But unlike for example serverStatus I can't do db.command('stats'). I was not able to find any API equivalent in mongodb docs. I've also tried variations with db.$cmd but none of that worked.
So,
Small question: how can I get results of db.stats() (number of connections/objects, size of data & indexes, etc) in my python code?
Bigger question: can anyone explain why some of shell commands are easily accessible from API, while others are not? It's very annoying: some admin-related tools are accessible via db.$cmd.sys, some via db.command, some via ...? Is there some standard or explanation of this situation?
PS: mongodb 2.0.2, pymongo 2.1.0, python 2.7
The Javascript shell's stats command helper actually invokes a command named dbstats, which you can run from PyMongo using the Database.command method. The easiest way to find out what command a shell helper will run is to invoke the shell helper without parentheses -- this will print out the Javascript code it runs:
> db.stats
function (scale) {
return this.runCommand({dbstats:1, scale:scale});
}
As for why some commands have helpers and others do not, it's largely a question of preference, time, and perceived frequency of use by the driver authors. You can run any command by name with Database.command, which is just a convenience wrapper around db.$cmd.find_one. You can find a full list of commands at List of Database Commands. You can also submit a patch against PyMongo to add a helper method for commands you find that you need to invoke frequently but aren't supported by PyMongo yet.