mixed public DsMap::get (mixed $key [, mixed $default])Parameter:This function takes two parameters as above and described below:
- $key:contains the key whose value will be returned.
- $default:this is an optional parameter that is returned if the key is not found.
// PHP illustration program get()
// Ds / map function
// Create a map
$map
=
new
DsMap ([
1 = > 10,
2 = > 20,
3 = > 30,
4 = > 40,
5 = > fifty]);
// Use the function Ds / Map::get()
var_dump (
$map
-> get ( one));
var_dump (
$map
-> get (3));
var_dump (
$map
-> get (5));
var_dump (
$map
-> get (7, 100));
?>
Exit:int (10 ) int (30) int (50) int (100)
Program 2: < ? php
// Create a map
$map
=
new
DsMap ([
"a"
= >
"Geeks"
,
" b "
= >
" for " ,
"c"
= >
" Geeks "
]);
// Use the function Ds / Map::get()
var_dump (
$map
-> get (
"a"
));
var_dump (
$map
-> get (
"b"
));
var_dump (
$map
-> get (
"c"
));
var_dump (
$map
-> get (
"d"
,
"GeeksforGeeks"
));
?>
Exit:string (5 ) "Geeks" string (3) "for" string (5) "Geeks" string (13) "GeeksforGeeks"
Link: https://www.php.net/manual/en/ds-map.get.php
PHP Ds / Map get () function: StackOverflow Questions
InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately
Tried to perform REST GET through python requests with the following code and I got error.
Code snip:
import requests
header = {"Authorization": "Bearer..."}
url = az_base_url + az_subscription_id + "/resourcegroups/Default-Networking/resources?" + az_api_version
r = requests.get(url, headers=header)
Error:
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:79:
InsecurePlatformWarning: A true SSLContext object is not available.
This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail.
For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
My python version is 2.7.3. I tried to install urllib3 and requests[security] as some other thread suggests, I still got the same error.
Wonder if anyone can provide some tips?
Answer #1:
The docs give a fair indicator of what"s required., however requests
allow us to skip a few steps:
You only need to install the security
package extras (thanks @admdrew for pointing it out)
$ pip install requests[security]
or, install them directly:
$ pip install pyopenssl ndg-httpsclient pyasn1
Requests will then automatically inject pyopenssl
into urllib3
If you"re on ubuntu, you may run into trouble installing pyopenssl
, you"ll need these dependencies:
$ apt-get install libffi-dev libssl-dev
Answer #2:
If you are not able to upgrade your Python version to 2.7.9, and want to suppress warnings,
you can downgrade your "requests" version to 2.5.3:
pip install requests==2.5.3
Bugfix disclosure / Warning introduced in 2.6.0
Dynamic instantiation from string name of a class in dynamically imported module?
In python, I have to instantiate certain class, knowing its name in a string, but this class "lives" in a dynamically imported module. An example follows:
loader-class script:
import sys
class loader:
def __init__(self, module_name, class_name): # both args are strings
try:
__import__(module_name)
modul = sys.modules[module_name]
instance = modul.class_name() # obviously this doesn"t works, here is my main problem!
except ImportError:
# manage import error
some-dynamically-loaded-module script:
class myName:
# etc...
I use this arrangement to make any dynamically-loaded-module to be used by the loader-class following certain predefined behaviours in the dyn-loaded-modules...
Answer #1:
You can use getattr
getattr(module, class_name)
to access the class. More complete code:
module = __import__(module_name)
class_ = getattr(module, class_name)
instance = class_()
As mentioned below, we may use importlib
import importlib
module = importlib.import_module(module_name)
class_ = getattr(module, class_name)
instance = class_()
Answer #2:
tl;dr
Import the root module with importlib.import_module
and load the class by its name using getattr
function:
# Standard import
import importlib
# Load "module.submodule.MyClass"
MyClass = getattr(importlib.import_module("module.submodule"), "MyClass")
# Instantiate the class (pass arguments to the constructor, if needed)
instance = MyClass()
explanations
You probably don"t want to use __import__
to dynamically import a module by name, as it does not allow you to import submodules:
>>> mod = __import__("os.path")
>>> mod.join
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: "module" object has no attribute "join"
Here is what the python doc says about __import__
:
Note: This is an advanced function that is not needed in everyday
Python programming, unlike importlib.import_module().
Instead, use the standard importlib
module to dynamically import a module by name. With getattr
you can then instantiate a class by its name:
import importlib
my_module = importlib.import_module("module.submodule")
MyClass = getattr(my_module, "MyClass")
instance = MyClass()
You could also write:
import importlib
module_name, class_name = "module.submodule.MyClass".rsplit(".", 1)
MyClass = getattr(importlib.import_module(module_name), class_name)
instance = MyClass()
This code is valid in python ‚â• 2.7 (including python 3).
pandas loc vs. iloc vs. at vs. iat?
Recently began branching out from my safe place (R) into Python and and am a bit confused by the cell localization/selection in Pandas
. I"ve read the documentation but I"m struggling to understand the practical implications of the various localization/selection options.
Is there a reason why I should ever use .loc
or .iloc
over at
, and iat
or vice versa? In what situations should I use which method?
Note: future readers be aware that this question is old and was written before pandas v0.20 when there used to exist a function called .ix
. This method was later split into two - loc
and iloc
- to make the explicit distinction between positional and label based indexing. Please beware that ix
was discontinued due to inconsistent behavior and being hard to grok, and no longer exists in current versions of pandas (>= 1.0).
Answer #1:
loc: only work on index
iloc: work on position
at: get scalar values. It"s a very fast loc
iat: Get scalar values. It"s a very fast iloc
Also,
at
and iat
are meant to access a scalar, that is, a single element
in the dataframe, while loc
and iloc
are ments to access several
elements at the same time, potentially to perform vectorized
operations.
http://pyciencia.blogspot.com/2015/05/obtener-y-filtrar-datos-de-un-dataframe.html