Dynamically Creating u-strings in Python - python-2.x

https://docs.python.org/2/tutorial/introduction.html#unicode-strings
I'm currently trying to figure out how to dynamically create u-strings (which are in the form of u'helloWorld' for example). I'd like to create a u-string with string concatenation or string injection if it were possible, to use a variable to dynamically create a new u-string such as u'{variableName}'. Is there a way to accomplish this?
The usage of the Python 2 u-strings are for usage of the gcloud functions, where I'm trying to dynamically add documents. The documentation makes use of u-strings and can be found at:
https://firebase.google.com/docs/firestore/manage-data/add-data

u-strings don't exist anymore in Python 3. They are the same as normal strings. So just use normal strings.
# Python 3
>>> u'Hello'
'Hello'
Do not use Python 2 as it is no longer supported. If you insist on using Python 2, try:
# Python 2
>>> unicode('Hello')
u'Hello'

Related

Python: Distributed computing on cloud for a python function

I have a Python function that is as simple as shown below. However, processing it one by one will take far too long.
So I'm considering splitting the input 'list of ids' into multiple lists in order to run it on a cloud parallelly. I believe AWS 'EC2' is one option, but configuring them one by one is too complexed. I'm wondering if there's any simple way to speed up my work?
import many_packages
def myfunc(list_of_ids:List[int])->None:
new_file = run preprocessing_with_pandas()
# this stage needs a java run time and python's subprocess
results = run_a_jar_with(new_file)
upload_results_to_s3()
Expected result:
many_lists = list_of_ids.split_into_chunks(chunk_size=100)
for i,c in enumerate(computers:"Cloud Instance"):
c.computing(myfunc(many_lists[i]))
The current constraint is that I cannot use 'pyspark' because the data must be processed using a library that only supports pandas. So I'm doing research on another framework: 'Dask' to see how feasible it can be done with it.

Ruby: how to access group info from /var/db/group.db

RHEL/CentOS 8.x, Ruby 2.5.5, irb 0.9.6
We have a specialized set up that keeps user and group information in /usr/local/etc/[user|group] and a custom Makefile to add that information to /var/db/[group|passwd].db accordingly.
I am trying to get the list of groups a user belongs to, in Ruby, after login.
I'm relatively new to the language, and have just read the documentation for the Etc module but that seems to exclusively work with the /etc/ filesystem. Not outrageous tbh.
Is there an easy way to access the Berkley DB info in /var/db or am I going to have to iterate through the /usr/local/etc/group file to get this information?
I suspect that the documentation of that module is heavily simplified, heavily outdated, or both. I am almost 100% sure that Etc will use the OS-provided standard library functions instead of going off and parsing system files by itself. (Why would the Ruby developers write parsers for the configuration files of every single OS they support, instead of just calling a POSIX function?)
You can confirm this suspicion using strace.
If you look at how the Ruby Etc module is structured, it maps 1:1 to the POSIX functions:
Etc::gegtrent: gets the next group entry.
Etc::endgtrent: stops iterating the groups.
Etc::settrent: resets the iteration.
Here are the POSIX functions for comparison:
endgrent, getgrent, setgrent – group database entry functions
In my case, I am testing this on macOS (which already has specialized user handling), but furthermore, I tested it on my company laptop, which is macOS joined to a Microsoft ActiveDirectory Domain. My user account and its group aren't even mentioned in either /etc/passwd or /etc/group, and yet, I can easily read them with Etc. I am very sure that the Ruby developers did not implement ActiveDirector just to accommodate my personal weird use case, so it must use system functions.
So, if you have e.g. a uid and want to know what groups it belongs to, you need to first get its name, and then search the groups. Unfortunately, it looks like the provided iterator wrappers do not support the common idiom that not supplying the block returns an Enumerator, so you have to create one yourself.
require 'etc'
uid = 0 # for example
username = Etc.getpwuid(uid).name
groups = Etc.enum_for(:group).select {|group| group.mem.include?(username) }
p groups

Pass parameter to bash script and call bash scripts in Chef cookbook

I have bash script to creating stores in openstack. My scenarios is like below:-
Parameter is passed along cookbook when execute them , this is a store name like store 1 , store 2
For example :- If i want to create a store 5 then i will call cookbook named "store-automation-cookbook " along with store name store 5, this store name in-turn will be passing to BASH script
then BASH script will take care rest of the automation
I answered this on the mailing list too so just copying over my answer verbatim. In the future please don't cross post like this.
Chef doesn't really have a good way to take command line input like that. Using -j as mentioned is possible but you might end up setting more things that you meant to. Using environment variables is another option but only works from the command line for the most part. A hybrid of the two might be best for you, consume the env var if set, otherwise use a node attribute.

Alternative script for YAML?

I was going to use yaml because it has great feature called merge! ("<<" key)
And I'm using 'yaml-cpp' for parser since i'm working on cpp.
But! yaml-cpp does not support merge. What can I do for alternatives?
Other scripts, other parser, other way to parse or whatever is good if I can use merge feature.
BUT I don't need to merge more than one object. I just need define something and create another object inheritd from the first one and override some values. That it.
Thanks for reading.
If you're unable to wait and need merges, you can follow the suggestion by "barma" on the yaml-cpp issue: http://code.google.com/p/yaml-cpp/issues/detail?id=41#c12
The change is to insert the lines below into FindValueForKey template (between for-loop and return 0):
const Node *pValueMerge = FindValueForKey(std::string("<<"));
if(pValueMerge) {
return pValueMerge->FindValueForKey(key);
}
The problem (as I mentioned on the issue page) is that the spec allows
<<: [*dict1, *dict2]
to merge multiple dictionaries; but it appears you don't need that.
Ask 'yaml-cpp' to implement the feature.
Problem
Using YAML merge keys.
Solution
Other scripts, other parser, other way to parse or whatever is good if I can use merge feature.
The following YAML implementations support the desired feature as of this writing
Ruby 2.x
Python 2.x // 3.x

Using Apache and mod_ext_filter, need to dynamically replace values of static file based off of query string

I've got a situation where I need to alter the contents of a cached file based off of one of the query string arguments passed in. I'd love to use sed to do a simple regular expression replacement of a value based off of said argument but I can't figure that one out. I could use a ruby script to do the replacement for me but can't seem to access the query string for the request within the script. The documents for mod_ext_filter say:
In addition to the standard CGI environment variables, DOCUMENT_URI, DOCUMENT_PATH_INFO, and QUERY_STRING_UNESCAPED will also be set for the program.
Um yeah, can't seem to access those.
Has anybody any experience with this or does anybody have a better solution?
Doh! Looks like I simply need to access the ENV variable within ruby. Pretty dumb of me.
Using PHP scripting language server function we can able to get the query string values.
echo $_SERVER['REQUEST_URI'];
And pass the URL arguments as a variable to the file and make it dynamic.
Refer : PHP.net

Resources