Call function based on template argument type

Question: There are two "C" functions:
void fooA(const char*);
void fooW(const wchar_t*);

Then there is a wrapper template function:
template<typename _TChar>
void foo(const _TChar* str)
{
     // call fooA or fooB based on actual type of _TChar
     // std::conditional .. ?
         // fooA(str);
         // fooW(str);
}

If the caller calls foo("Abc"), this template function should make a compile-time call to fooA.  Similiarly, foo(L"Abc") should make the final call to fooW.
How do I do that? I thought of using std::conditional but couldn't make it.
I cannot make fooA or fooB overloaded, since these are C functions.
Answer: You can put all your wchar_t versions in a class template, say overloads and their char counter-parts in its specialization as shown below:
template<typename WideCharVersion>
struct overloads
{
    void foo(wchar_t const * arg)
    {
       FooW(arg);
    }
    //more wchar_t functions
};

template<>
struct overloads<std::false_type>
{
    void foo(char const * arg)
    {
       FooA(arg);
    }
    //more char functions
};

//a friendly alias!
template<typename T>
using is_wide_char = typename std::is_same<whar_t, T>::type;

And then you can use them as:
template<typename _TChar>
void foo(const _TChar* str)
{
    overloads<is_wide_char<_TChar>>::foo(str);
}

Expression SFINAE makes it easy!
Other way is to use Expression SFINAE which does not require to you write anything like overloads and does the same job with less code:
template<typename _TChar>
void foo(const _TChar* str)
{
    invokeOne(fooA, fooW, str);
}

And then you can implement invokeOne as:
 template<typename F1, typename F2, typename ... Args>
 auto invokeOne(F1 f1, F2 f2, Args && ... args) -> decltype(f1(args...))
 {
     return f1(args...);
 }

 template<typename F1, typename F2, typename ... Args>
 auto invokeOne(F1 f1, F2 f2, Args && ... args) -> decltype(f2(args...))
 {
     return f2(args...);
 }

Have a look at the online demo.
In this approach, you don't have to add the overloads to the overloads class template and to its specialization. Instead you just pass them as arguments to invokeOne which calls the right overload for you.
Hope that helps.
Comment: In what aspect is this better than the solution of lego and IanM_Matrix1?
Comment: @tamas.kenez: The first approach is similar. However, the SFINAE approach is different from their approach, which requires lesser code to get things done.
Answer: Then overload another function. I'm assuming foo does more work and needs to be a template. Then call foo_forward_call, defined as such:
void foo_forward_call(char const* ptr) {
    FooA(ptr);
}

void foo_forward_call(wchar_t const* ptr) {
    FooW(ptr);
}

and at the call site:
template<typename _TChar>
void foo(const _TChar* str)
{
    foo_forward_call(str);
}

in C++1z you'll be able to use constexpr if, but to be honest, I think that overloaded solution is still more readable.
template<typename _TChar>
void foo(const _TChar* str)
{
    if constexpr(std::is_same<_TChar, char>::value) {
        FooA(str);
    } else {
        FooW(str);
    }
}

Alternatively, you can use Boost.Hana's overload:
template<typename _TChar>
void foo(const _TChar* str)
{
    hana::overload(fooA, fooW)(str);
}

demo

By the way: you should eschew using underscore-capital-letter names in your programs. They're reserved for implementation for any use (i.e. macros) and might lead to nasty name clashes.
Answer: This seems like a really strange thing to do with templates. I would suggest using normal overloading instead:
void foo(const char* p) { fooA(p); }
void foo(const wchar_t* p) { fooW(p); }

If you insist on using template then you can do it like this:
template <typename T>
void foo(const T* p)
{
    // Declare functions here so that calling fooW with const char*
    // and 'calling' fooA with const wchar_t* would not cause compile error.
    void fooA(const T*);
    void fooW(const T*);

    if (std::is_same<char, T>::value)
        fooA(p);
    else
        fooW(p);
}

Comment: The linker will try to resolve *both* branches `if-else` and one of them will cause linker-error. If `T` is `char`, `fooW` will cause linker-error, else `fooA`.
Comment: True, but not always. This works with gcc and clang, not with vs. To fix this for vs, empty overloads should be defined, but then why bother with all this template stuff, just use plain overload.
Answer: I like solving problems in general.  So let us design a mechanism to overload stuff.
overload_t<...> takes a set of callables in ... and generates an object which uses standard overload resolution to choose between them, via inheritance of operator():
template<class...Fs>
struct overload_t;
// the case where we have a function object:
template<class F>
struct overload_t<F>:F{
  overload_t(F f):F(std::move(f)){}
  using F::operator();
  // boilerplate to ensure these are enabled if possible:
  overload_t(overload_t&&)=default;
  overload_t(overload_t const&)=default;
  overload_t& operator=(overload_t&&)=default;
  overload_t& operator=(overload_t const&)=default;
};
// we cannot inherit from a function pointer.  So
// store one, and write an `operator()` that forwards to it:
template<class R, class...Args>
struct overload_t<R(*)(Args...)>{
  using F=R(*)(Args...);
  F f;
  overload_t(F fin):f(fin){}
  R operator()(Args...args)const{
    return f(std::forward<Args>(args)...);
  }
  overload_t(overload_t&&)=default;
  overload_t(overload_t const&)=default;
  overload_t& operator=(overload_t&&)=default;
  overload_t& operator=(overload_t const&)=default;
};
// the case where we have more than type to overload.
// recursively inherit from the one-arg and the rest-of-arg
// and using operator() to bring both of their () into equal standing:
template<class F0, class...Fs>
struct overload_t<F0,Fs...>:
  overload_t<F0>,
  overload_t<Fs...>
{
  using overload_t<F0>::operator();
  using overload_t<Fs...>::operator();
  overload_t(F0 f0, Fs...fs):
    overload_t<F0>(std::move(f0)),
    overload_t<Fs...>(std::move(fs)...)
  {}
  overload_t(overload_t&&)=default;
  overload_t(overload_t const&)=default;
  overload_t& operator=(overload_t&&)=default;
  overload_t& operator=(overload_t const&)=default;
};
// a helper function to create an overload set without
// having to specify types.  Will be obsolete in C++17:
template<class...Fs>
overload_t<Fs...> overload(Fs...fs){ return {std::move(fs)...};}

Now to generate a single object that is an overload of multiple, do this:
overload(FooA,FooW)( str );

And str will be dispatched to either based on the usual overload resolution rules.  This is useful elsewhere which is why it is worth writing, and the code at point of use is self documenting.
Live example (wow, wrote it correct the first time!)
There are a number of improvements one can add to the above overload_t.

Perfect forwarding of the fs during construction and in the helper function.
Balanced Binary tree inheritance instead of linear (important of more than a few overloads are done).  This can have runtime and compiletime performance impliciations, especially for a large number of functions.
Introspect incoming Fs; if they are overload_t balance the combined tree.
In C++17, a func<auto> template that takes a function pointer and returns a function object that statelessly invokes it.  Compilers are relatively good at eliding function pointers, but they are better at it when there is no possible runtime state that could alter them.
Deciding what to do for overload_t<>.  Currently it fails to compile; maybe it should just be an empty struct {}, or even a struct with an uncallable operator().
Examine existing libraries, like boost::hana::overload and see what differences there are.
Expose the ability to extract which of the overloads would be called, maybe via a static tag_t<F> which_overload_helper( Args... ) const method and template<class...Args> using which_overload = typename decltype( which_overload_helper( std::declval<Args>()... ) )::type;
Properly picking overloads when some of the incoming Fss do/do not have a const operator().  Should the function pointer have const, volatile, both or neither on operator()?  All 4?  How about && vs & overloads?
Answer: Maybe if there were more targetted functions or overloading wasn't the only problem you were trying to solve then it might be worthwhile using templates to get this done.
In this case though, simply write your overloads:
void foo(const char* x) { fooA(x); }
void foo(const wchar_t* x) { fooW(x); }

Comment: This is what I've done finally. But any change in one function requires change in another one also.
Answer: You have a few options.

Use an helper explicitly specialized struct:
template <typename>
struct helper;

template<>
struct helper<char>
{
    void operator()(const char* x){ FooA(x); }
};

template<>
struct helper<wchar_t>
{
    void operator()(const wchar_t* x){ FooW(x); }
};

template <typename _TChar>
void foo(const _TChar* str)
{
    helper<_TChar>{}(str);
}

Use a "static if" implementation (e.g. boost::hana::eval_if or my own):
template <typename _TChar>
void foo(const _TChar* str)
{
    vrm::core::static_if(std::is_same<_TChar, char>{})
        .then([](const auto* x_str){ FooA(x_str); })
        .else_([](const auto* x_str){ FooW(x_str); })(str);
}

Use an helper overloaded function:
void helper(const char* x) { FooA(x); }
void helper(const wchar_t* x) { FooW(x); }

template <typename _TChar>
void foo(const _TChar* str)
{
    helper(str);
}

Comment: I think `.then(FooA).else_(FooW)` should work as well? If not, the designer of the library should make it work as well!
Comment: @Nawaz: it's more complicated - every branch requires needs to be a template that will only be instantiated if the condition matches. If it's not a template, compilation will fail. [I discussed this in my CppCon 2016 talk](https://www.youtube.com/watch?v=aXSsUqVSe2k).
Comment: I'm sure it is complicated. Still I feel that it can be made to work, by adding one more level of indirection.
Answer: I think using typeid() is very elegant and readable, but not sure if there is any side effect.
#include <typeinfo>

template <typename T>
void foo(){
    if(typeid(T) == typeid(const char*)){
        fooA();
    }
    else if (typeid(T) == typeid(const wchar_t*)){
        fooB();
    }
}

Check it out in wandbox.
Comment: Thanks for adding the answer. But this doesn't really answer the question. It uses `typeid` which may require RTTI. Even if the type is determined at compile time, the `if-else` is not compile time expression.  templates are for compile time code, typeid goes towards runtime-type information and evaluation. See `if-constexpr` also.


Remap properties name and values using lodash

Question: I have this array:
aItems = [{
    "PropertyA": "apple",
    "PropertyB": "banana",
    "PropertyC": "dog",
    "PropertyD": "hotdog",
    "PropertyE": "coldcat",
    "PropertyF": "Y",
    "PropertyG": "N"
},
...,
{
    "PropertyA": "this",
    "PropertyB": "is",
    "PropertyC": "json",
    "PropertyD": "code",
    "PropertyE": "wow",
    "PropertyF": "N",
    "PropertyG": "N"
}]

I would like use lodash to obtain this result:
aItems = [{
    "propertyA": "apple",
    "propertyB": "banana",
    "propertyC": "dog",
    "propertyD": "hotdog",
    "propertyE": "coldcat",
    "propertyNEW": true,
    "propertyG": false
},
...,
{
    "propertyA": "this",
    "propertyB": "is",
    "propertyC": "json",
    "propertyD": "code",
    "propertyE": "wow",
    "propertyNEW": false,
    "propertyG": false
}]

I want map each property name with other names and change the value for some specific properties.
Can I do it using lodash?
Answer: Yes, since lodash v3.8.0 you can remap objects in any way desireable
ES5 code
var items = [ { oldKey: 'oldValue' /*...*/ } ]

var keyMapping = { oldKey: 'newKey' /*...*/ }
var valueMapping = { oldValue: 'newValue' /*...*/ }

var remapper = function(item){
  return _(item) // lodash chain start
    .mapKeys( function(v, k){ return keyMapping[k] } )
    .mapValues( function(v){ return valueMapping[v] } )
    .value() // lodash chain end
}

var remappedItems = items.map(remapper)

ES2015/ES6 code
let items = [ { oldKey: 'oldValue' /*...*/ } ]

let keyMapping = { oldKey: 'newKey' /*...*/ }
let valueMapping = { oldValue: 'newValue' /*...*/ }

let remapper = item => _(item) // lodash chain start
  .mapKeys( (v, k)=> keyMapping[k] )
  .mapValues( v => valueMapping[v] )
  .value() // lodash chain end

let remappedItems = items.map(remapper)

Comment: If you want all the key pairs and only want to change one or two keys and don't want to list all the keys in keyMapping, you can change:    `.mapKeys( (v, k)=> keyMapping[k] )`    to   `.mapKeys( (v, k)=> keyMapping[k] || k)`
Answer: Create a mapping of old and new keys, like this
var keyMapping = {'PropertyA': 'propertyA', ..., 'PropertyF': 'propertyNEW'}

and also a mapping of old and new values, like this
var valueMapping = {'Y': true, 'F': false}

And then using _.map and _.transform, you can transform the object, like this
var result = _.map(allItems, function(currentObject) {
    return _.transform(currentObject, function(result, value, key) {
        if (key === 'PropertyF' || key === 'PropertyG') {
            value = valueMapping(value);
        }
        result[keyMapping[key]] = value;
    });
});


CDF Variable Transformation

Question: Let $X$ be uniform on $(-1, 2)$ and let $Y = X^2$. Find the pdf of $Y$.
So far I have noted that $F_X(x) = P(X \leq x) = \int_{-1}^x \frac{1}{3} dt = \frac{1}{3}(x+1)$.
Then, since $Y=X^2$, $y \in [0,4]$.
My initial attempt was to do the normal procedure of
$F_Y(y) = P(Y \leq y) = P(X^2 \leq y) = \begin{cases} P(X \geq -\sqrt{y}, \quad x \in [-1,0) \\ P(X \leq + \sqrt{y}, \quad x \in [0,2]  \end{cases}$
Continuing,
$F_Y(y) = \begin{cases} F_X(-\sqrt{y}), \quad x \in [-1,0) \\ F_X(+\sqrt{y}), \quad x \in[0,2] \end{cases} = \begin{cases} \frac{1}{3}(1-\sqrt{y}), \quad x \in[-1,0) \\ \frac{1}{3}(1+\sqrt{y}), \quad x \in[0,2] \end{cases}$
I'm fairly happy that the CDF of $Y$ is continuous and well defined but I don't like the fact that I need to specify the x domain since it should after all be a function of y, right? Or is it necessary in this case since $Y=X^2$ is not one-to-one on the domain?
Comment: It might be worth considering $F_Y(y)$ when $y$ is (a) below $0$, (b) between $0$ and $1$, (c) between $1$ and $4$, and (d) above $4$
Comment: You should be able to write the final answer only using $y$ restrictions.
Answer: There is an error in your calculation (it seems you would get $F_Y(0) = 1/3$, but this probability should be $0$). Note that (for $y\geq 0$), $P(X^2\leq y)$ is actually equal to $P(-\sqrt{y}\le X \le \sqrt{y}) = \color{red}{F_X(\sqrt{y}) - F_X(-\sqrt{y})}$. Try and compute this red part for $y\in [0,4]$. You will need to remember that $F_X(t)$ will become $0$ if $t < -1$.
Comment: OK. I now get $F_Y(y) = \begin{cases} \frac{2}{3} \sqrt{y} , \quad 0 \leq y \leq 1 \\ \frac{1}{3} \sqrt{y} + 1\ \quad 1 \leq y \leq 4 \end{cases}$. Does that seem right? I took into account the restricted domain of $F_X(-\sqrt{y})$ so it makes sense mathematically but I am struggling to get intuition on this. I think we can understand it as the coefficient is twice as high in $0 \leq y \leq 1$ region since both $-1 \leq x < 0$ and $0 \leq x \leq 1$ regions are contributing, whereas for $y \in (1,4]$, only 1 part of $x$ domain is contributing. The $+1$ constant adds the area already accumulated
Comment: The part for $1\le y\le 4$ has a typo I think (needs brackets). Otherwise looks good.

CDF Variable Transformation


How to replace string "var num = ['asd23','asd31']" in powershell

Question: Replace the give string
var num = ['red222','edr223']; //this value is always changing

as below
var num = ['rev11111','rev222']; //replace with any string

Tried:
num = @('rev11111','rev222') // text present in file say file.txt
(Get-Content "C:\file.txt").Replace('var num = *',"var num = ["+(@($Num) -join ",")+"];" )| Set-Content "C:\file.txt"

This did not help much. It cant replace when the [ ] are not empty. How to replace the string. Please help!
Answer: I think you've to use the -replace operator and not the Replace-method of the string object. Since Replace doesn't support Regex.
If you change to:
$Num = @('rev11111','rev222') # text present in file say file.txt
(Get-Content "C:\temp\input.txt") -replace ('var num = \[.*\]', "var num = [$(@($Num) -join ",")]") | Set-Content "C:\file.txt"

it seems to work. I also have extended the Regex to var num = \[.*\].
Hope that helps.
Comment: .Net's [`System.String`](https://learn.microsoft.com/en-us/dotnet/api/system.string?view=netframework-4.7.1#Methods) doesn't support regex. There's a class in `System.Text.RegularExpressions`, but using it is quite overkill when `-replace` works as per your answer.
Comment: @nitin chawda: Please mark the question as solved, if the answer is ok for you. Thx


Memory leak issue in Centos5

Question: I recently just bought a dedicated server as an upgrade for a small community site from the shared hosting we have had for the last 3 years. I had installed httpd, php and mysql (the usuals for a webserver). I configured httpd for a few virtual hosts and installed webmin and cacti. Everything seemed to run pretty smooth until i noticed something off in the graph for the memory usage in cacti.
My server has 2gig RAM and for some reason 1.7gig of it was in use. I logged in as root through SSH and rebooted the server and then ran 'top'. Straight away it shot up to using 600meg RAM and kept slowly increasing but it didn't look anything was using the RAM. I ran 'ps aux'. But for some strange reason, it looks as if no running app is using over 2% ram. If i add up all the mem usage from 'ps aux' it comes out to about 20.6% which i would assume is close to 400meg RAM, this being when over 800meg RAM was in use.
So, it somewhat seems to me as if around 400meg of ram was being used where no application was claiming it. Over time, the memory slowly creeps up until it uses all available RAM. Though, i haven't let it get to that point yet.
I'm not really sure how to figure out what to do next to try and rectify the issue. Any help at all would be appreciated.
Memory usage graph from cacti:

Output of 'ps aux' & 'free -o'
Let me know if any more information is needed.
Comment: ...this really should be a FAQ.
Comment: I can't say i didn't look around. I looked for hours on end, but i never thought of kernel disk caching, it's... completely new to me. I tried wording my issue in many ways to see what google would come up with, but i never though of linux being the one to 'use' the memory. I really just thought it was httpd/php, despite top/ps not showing it as using much.
Answer: First, check this page: Linux ate my RAM.
You haven't described any behavior that indicates a memory leak, at least none that I can percieve. As explained in the above page, the kernel will use free RAM as a disk read cache. Bytes allocated as cache will be freed up by the kernel immediately to any application that needs it.
Try running free instead of -o. When you do this, you will be provided a value of RAM available, taking into consideration of what is currently being used for cache. Here's some sample output from one of my servers:
user@host:~$ free
             total       used       free     shared    buffers     cached
Mem:        510652     430976      79676          0     134520     156876
-/+ buffers/cache:     139580     371072
Swap:       262136      40796     221340

See the -/+ buffers/cache line? The value in the free column reflects the actual amount of RAM that is available to your apps.
Comment: Oh wow, i had no idea the linux does that. That makes a lot of sense now that. When i did 'free' (without the -o), it shows that i have 380meg used in the buffers/cache section with 1.6gig actually free.

So yeah, turns out i actually didn't have any memory issues at all. Thankyou very much ErikA, really appreciate it.
Comment: You're welcome. Good luck!
Answer: Here is my Configuration:
IBM M3, Dual Quad Core, 8GB RAM, CentOS 5 x86_64.
We were running an application that was required to hold data in a vector and then drop it in a database. After running the application was a few days, we ran top and found that the memory utilisation was 99%+. This looked weird and I thought maybe CentOS 5 was using some kind of memory caching, but we could see a visible degradation in the performance of the server.
So I decided to run deep. We ran 2 terminal windows side by side to witness any evidence of bottlenecks. In one window we were printing data the speed/second our application was receiving the data and in the other window we ran a tcpdump trace for that particular port.
We found that the network card was running at an extremely high speed and was able to handle that data pretty well. but the Kernel was not able to take that data and give it to my application. The data would remain in a network queue and every 6-10 seconds, the application would receive a rush of data and it would drop again.
SOLUTION: Well I don't have anything for Cent OS 5. but we switched the OS to Fedora 14 x86_64. Memory utilisation was less than 1% and the kernel was able to process that data at extremely high speeds without any queueing. this is all I have and I don't know why CentOS didn't support this hardware architecture. our other CentOS installation works fine. But when we went for a dual multi core architecture, we hit this brick wall.
Hope this helps.
Thanks
Sid
Comment: Recommending moving from a server oriented release to a desktop oriented one is probably not the best solution for a server. With the number of differences between CentOS 5 (which was based on Fedora Core 6) and F14 there's no telling *what* fixed the problem.

Memory leak issue in Centos5


Show that $76^{2019} \equiv 45 \pmod{101}$

Question: Using Euler's theorem I see that $76^{2019} = 76^{19} \pmod{101}$, how do I proceed?
Comment: Iterated squaring isn't too hard.  You can use $76\equiv -25\pmod {101}$ if you prefer.
Answer: Here is an approach that requires minimum computation. We have:
$$76^{19} \equiv (-25)^{19} \equiv -5^{38} \pmod{101}$$
How we have that by quadratic reciprocity:
$$\left( \frac{5}{101} \right)=\left( \frac{5}{101} \right) \cdot \left( \frac{1}{5} \right) = \left( \frac{5}{101} \right) \cdot \left( \frac{101}{5} \right)=(-1)^{\frac{101-1}{2} \cdot \frac{5-1}{2}}=1$$
So $5$ is the quadratic residue modulo $101$ and therefore $5^{50} \equiv 1 \pmod{101}$. So we can write:
$$-5^{38} \equiv -5^{-12} \equiv -25^{-6}\pmod{101}$$
And since $25 \cdot (-4) \equiv -100 \equiv 1 \pmod{101}$ we have $25^{-1} \equiv -4 \pmod{101}$ and therefore:
$$-25^{-6} \equiv -(-4)^6 \equiv -4^6 \equiv -4096 \equiv 45\pmod{101}$$
Comment: Nice solution, I didn't think of using quadratic reciprocity.
Comment: When your exponent is close to half of $p-1$ you can use it to determine the value of $\frac{p-1}{2}$th power (which is always $1$ or $-1$) and therefore reduce the exponent significantly.
Answer: $101$ is prime, so $76^{100}\equiv1\pmod{101}$ by Fermat's little theorem, so
$76^{2019}\equiv76^{19}\pmod{101}$.
$76^2\equiv19\pmod{101}$.  $76^4\equiv58\pmod{101}$. $76^8\equiv 31\pmod{101}$.  $76^{16}\equiv 52\pmod{101}$.
Therefore $76^{19}\equiv76^{16}76^2 76^1\equiv52\times19\times76\equiv45\pmod{101}$.
Answer: $101$ is prime so $\mathbb Z_{101}$ is a field.
$76\equiv -25 \equiv -100\cdot\frac 14\equiv \frac 14$
So $76^{19} \equiv (\frac 14)^{19}\equiv (\frac 14)^{20}*4$
$4^5 \equiv 1024 \equiv 14$
$4^{10} \equiv 14^2 \equiv (10 + 4)^2 \equiv 100 +80 + 16 \equiv -1-21+16 \equiv 6$.
$4^{20} \equiv 6^2 \equiv 36$.
So $76^{19} \equiv \frac 1{36}*4 = \frac 19$.
$9*11 \equiv 99 \equiv -2$
$9*11*50 \equiv -100 \equiv 1$.
So $76^{19} \equiv \frac 19 \equiv 550\equiv 45 \pmod {101}$.
=======
Old method:
So we know $76^{100}\equiv 1 \pmod {101}$
And we know that the order of $76$ will divide $100$.
$19$ is one less than $20$ and $20|100$ and that gives me a really strong hunch that $76^{20}$ may be congruent to $1$ or a "fifth root" of $1$.  In any event my gut says it's worth calculating $76^{20}$.
$76^2\equiv (-25)^2 \equiv 625 \equiv 606 + 19\equiv 19 \pmod{101}$
$19^5 = (20-1)^5 = 20^5 - 5*20^4 + 10*20^3 - 10*20^2 + 5*20 - 1$.
Now note $100 \equiv -1$ so
$10^3 \equiv -10$, $10^4 \equiv 1$ and $10^5\equiv 10$.
So
$76^{10}\equiv 19^5 \equiv 20^5 - 5*20^4 + 10*20^3 - 10*20^2 + 5*20 - 1\equiv$
$32*10 -5*16 +10*8*(-10) - 10*(-4) + 100 - 1\equiv$
$320 - 80 +8 +40 -1 -1 \equiv$
$17 + 21 + 8 + 38 \equiv 84\pmod {101}$
So $76^{20} \equiv 84^2 \equiv (-17)^2 \equiv (20-3)^2 \equiv 400 - 120 +9\equiv 289 = 303 - 14\equiv -14\equiv 87\pmod {101}$
So we need to find the inverse of $76$.
$76 = 50 + 25 + 1$.
$2*76 = 100 + 50 + 2\equiv -1 + 50 + 2 \equiv 51$.
$4*76 \equiv 2(50 + 1) \equiv 100 + 2 \equiv 102 \equiv 1\pmod {101}$ so
$76^{-1}\equiv 4 \pmod {101}$.
So $76^{19} \equiv 76^{20}*76^{-1} \equiv 87*4 \equiv 348 \equiv 45 \pmod{101}$
Comment: Since $2^{-2}\equiv -5^2\,$ the first method is essentially the same as Bartek's answer, except that you compute $2^{-38}$ by brute-force instead of reducing it to $-2^{12},\,$ using $2^{50} \equiv -1$ by reciprocity.
Comment: Same answer.  Different intuitions of how to come up with it.
Comment: What is the "intuition" used above?
Comment: That $76\equiv -25$ and $25= \frac 14(100)$ and $100 \equiv -1$ so we can view $76$ as $\frac 14$.  And that as $20|100$ it might be easier to figure $76^{20}$ and multiply by $76^{-1}\equiv (\frac 14)^{-1}\equiv 4$.


Differential of a stochastic process $I(t) = e^{\sigma W(t)}$

Question: I need to compute the differential $dI$ of $I(t) = e^{\sigma W(t)}$ where $W(t)$ is a brownian motion.
With $I(W_t) = e^{\sigma W_t}$ I can apply Itô's lemma and get $dI =\frac{\partial I}{\partial W_t}dW_t +\frac{1}{2} \frac{\partial^2I}{\partial W^2_t}dt=\sigma e^{\sigma W_t}dW_t+\frac{1}{2}\sigma ^2e^{\sigma W_t}dt$.
With $I(t)$ how can I get $dI$?
Answer: This is just an application of Ito's calculus.
Let $dX_t=\tilde \mu(X_t)dt+\tilde\sigma(X_t)dW_t$ for some approprtiate functions $\tilde\mu,\tilde\sigma$. From Ito's formula we get that for some $C^2$ function $f$ we have that
$$df(X_t) = \dfrac{\partial f}{\partial x}dX_t+\dfrac{1}{2}\dfrac{\partial^2 f}{\partial x^2}\tilde{\sigma}(X_t)^2dt.$$
Now let $f(X_t)=\exp(\sigma X_t)$ with $X_t=W_t$, then we have that
$dX_t=dW_t$, and hence $\tilde\mu(\cdot)=0$ and $\tilde\sigma(\cdot)=1$. Therefore,
$$df(X_t) = \sigma \exp(\sigma X_t)dW_t+\dfrac{\sigma ^2}{2}\exp(\sigma X_t)dt.$$
If we let $I_t=f(X_t)$, we then obtain
$$dI_t = \frac{1}{2}\sigma ^2I_tdt + \sigma I_tdW_t,\ I_0=1$$
which is the equation of a geometric Brownian motion. You can verify that the formula is correct using the explicit formula for the solution of the geometric Brownian motion.
Comment: The notation $\frac{\partial I}{\partial W}$ is a bit misleading, as you are not taking the derivative with respect to Brownian motion, but rather the space variable in $I$.
Comment: Thank you. I can't seem to understand how to get the differential of $I(t)$ because the argument of $I$ is $t$ and not $W_t$. What am I missing?
Comment: @JoseAvilez Yes, that was hastily written with some bad copy-paste. I have now improved the answer.


Executing jQuery code during form submission

Question: I developed a module to show an "End" button on the node edit form, and it works fine.
function renew_form_alter(&$form, &$form_state, $form_id) {
  $form['actions']['property_endlease'] = array(
    '#type' => 'submit',
    '#value' => t('End'),
    '#access' => TRUE,
    '#weight' => 15,
  );

  return $form;
}

I want that, when users click on that button, some fields are changed from jQuery code.
I tried the following code, but it doesn't show any alert, even though the form is submitted.
jQuery('#edit-property-end').click(function (event) {
  alert('');
});

I want to submit the form after the jQuery code is executed.
Does anybody have an idea on how to do this?
Thanks
Answer: Try this
   $form['actions']['property_endlease'] = array(
            '#type' => 'submit',
            '#value' => t('End'),
            '#access' => TRUE,
            '#weight' => 15,
            '#attributes' => array("onclick" => "javascript:test();"),

        );

and create a custom js
function test()  {
//your code
}

Hope it will work


Can a Drupal Site Administrator view contents of settings.php or some other file in file system?

Question: We are using Drupal 8.6.3.
I want to store some configuration in file system (in settings.php or some other file outside Web Root).
Can a Drupal Site Administrator view that configuration?
What parts of settings.php file can be viewed by a Drupal Site Administrator?
Answer: For brevity, it helps to think about this question in terms of roles/permissions (since "site admin" is an overloaded term):
Does the ____ role have an escalated permission to:

Write arbitrary files to the server?
Execute arbitrary code on the server?

If the first/second answer is "Yes," the answer would be:
____ role could probably come up with a clever way to read the entire contents of settings.php by injecting/running arbitrary code.

Generally, Drupal core tries to signal these potentially dangerous permissions with this warning:

Warning: Give to trusted roles only; this permission has security implications.

However, there are many contrib modules out there that could have exploits (some known, some not) so you'll always have to take pro-active security steps like the principle of least privilege (if a role doesn't need a privilege to do the job, don't give it to them).
This also applies to your whole system (DB, web server, etc.), not just Drupal.
Comment: Also a site admin. may be able to _view_ variables that is stored statically in $conf if they have permission to view the user interface that configures that variable.


Solving for varible with use of ln

Question: Looking for some help with trying to solve for $x$ in the following equation,
$$0=5-re^{-x^2}$$
The solution is,
$$x^2=\ln\frac{r}{5}$$
Comment: Okay, and what's your question?
Comment: I am just looking for some help with the steps inbetween
Comment: This seems pretty straight forward,try to adjust the equation and take $\ln$.Also this question has nothing to do with system of equations.
Comment: We must have r>=5.
Answer: \begin{align*}
\implies 5 = re^{-x^2} \iff \frac{5}{r} = e^{-x^{2}} \iff \ln\frac{5}{r} = -x^{2} \iff -\ln\frac{5}{r} = x^2 \iff \ln \frac{r}{5} = x^{2}
\end{align*}
Note: $\ln a^{n} = n\ln a$. In this case, $n=-1$, i.e. $\ln a^{-1} = -\ln a$.
Comment: It's a property of logarithms. See my edit.
Comment: does the fraction of the ln switch because of the negative sign causing the ln5 to be negative?
Answer: There you go
0=5−re-x2
e-x2=5/r
ln(e-x2)=ln(5/r)
-x2=ln(5/r)
x2=-ln(5/r)
x2=ln[(5/r)-1]
x2=ln(r/5)

Solving for varible with use of ln


Cascade 2 routers with dhcp

Question: I'm wanting to cascade 2 routers, one is a Linksys E3000 Router, the other is a wireless Cable Modem/Router made by Arris.
As of now their cascaded via WAN to LAN, in other words, the internet port on the second router is plugged into an ethernet port on first router, everything works just fine.
With this setup there are 2 problems I'm trying to address and could use some help:
The most important thing is I need to switch over to LAN to LAN from WAN to LAN because I want open communication over the entire network and not have the 2 routers isolated on different subnets. The main problem with this is I still need DHCP on both routers and all the articles I've read have it disabled on the second router. I don't know why but If its a problem with DHCP fighting over addresses can't I have the second router assign addresses on ip 100 and up and the first ip 99 and below or something of the like?
The second issue is I'd like both routers, which also provide wireless, to have a shared SSID so that moving from one part of the house to the other doesnt mean having to switch SSID's or devices flipping back and fourth on different SSID's.
Any help would be much appreciated, thanks in advance.
**** EDIT
Sorry for being vague above,
I'm wanting to have 2 routers in a large house so that the whole house can be covered with both wireless and wired internet. I'm wanting the routers to be linked via ethernet so I can try and maintain maximum speed and help prevent any speed loss.
In the future I may add more routers to further expand wireless coverage, for example in the basement where wireless has trouble getting to, and also provide wired options for maximum speed so it needs to be expandable.
If possible I would like the 2 routers I have now, and future routers I may add later on to be all on the same subnet so it'll all be open and I won't have to worry about certain computers, devices, cell phones, etc in certain areas not being able to communicate with the others because their own different subnets as it is now.
Also it would simplify things as I may host a game server or website for the internet and I wouldn't have to worry with different subnet issues in getting to the outside world.
So I'm trying to get everything on the same subnet.
Since this in in a house, I cold have several guests over so it wont be very user-friendly if they have to give their devices a satic ip, especially since they may not even know what an ip is. Thats why its important to try and keep DHCP on all routers.
As for the SSID, I'm afraid to set all the routers, now and in the future, to the same SSID because they will overlap some and as of now are completely in their own world so I imagine it will just create confusion and choas for the router and device but  I could be wrong.
Comment: "*I still need DHCP on both routers.*" Why? You don't explain what your issue is.
Comment: "*The second issue is I'd like both routers, which also provide wireless, to have a shared SSID so that moving from one part of the house to the other doesnt mean having to switch SSID's or devices flipping back and fourth on different SSID's.*" Why is that an "issue"? If you want them to have the same SSID, just set the same SSID on them both. What's the issue exactly?
Comment: Sorry about the vague description, re-editied to be more clear, hope that helps
Comment: "*Since this in in a house, I cold have several guests over so it wont be very user-friendly if they have to give their devices a satic ip, especially since they may not even know what an ip is. Thats why its important to try and keep DHCP on all routers.*" That makes absolutely no sense. It's one LAN.
Answer: Once you make a LAN-to-LAN connection, it's all one LAN. You only want one DHCP server, and for convenience, that should be on the router with the Internet connection. If you want both routers on the same SSID, configure them that way, with the same SSID and security settings. Use different channels, if possible.
Comment: Thats good to hear, but if I disable the DHCP on all but one router because its all ultimately one LAN, will this also still work for wireless, or will wireless have to be a static ip.
Comment: It's all one LAN. The DHCP server will serve the LAN. You're using the routers as bridges, they don't care whether the traffic they're bridging is DHCP or not.
Comment: Thank you, that worked perfectly, I finally got a chance to configure and test it out. The whole thing worked flawlessly, including SSID and other stuff. Sorry for such a basic question but thanks a lot for the answer.

Cascade 2 routers with dhcp


virtual domain redirection working only partially with apache 2.2

Question: Apache 2.2.3 running with a lot of virtual hosts (boiled down to 3 Zope instances), configured in separate files.  Works quite well, with one exception:
The redirection of mydomain.net to www.mydomain.com works, while www.mydomain.net to www.mydomain.com doesn't work.
Currently the configuration file for these two looks like this:
<VirtualHost *:80>
  ServerAdmin firstname.lastname@example.com
  ServerName mydomain.net
  Redirect permanent / http://www.mydomain.com/
</VirtualHost>
<VirtualHost *:80>
  ServerAdmin firstname.lastname@example.com
  ServerName www.mydomain.net
  Redirect permanent / http://www.mydomain.com/
</VirtualHost>

I had a single vhost with ServerAliases before, and used a RedirectRule, which didn't work either.
/usr/sbin/apache2 -S now tells me:
     ...
     port 80 namevhost mydomain.net (/etc/apache2/common-file.conf:1)
     port 80 namevhost www.mydomain.net (/etc/apache2/common-file.conf:25)
Syntax OK

All vhosts have a :1 there, with the only exception of the non-working redirection. What do I make of this information; how can I debug this?
Comment: It seems the :nn numbers simply specify line numbers ...
Answer: Try changing your config to this and see
<VirtualHost *:80>
  ServerAdmin email@example.com
  ServerName mydomain.net
  ServerAlias www.mydomain.net
  Redirect permanent / http://www.mydomain.com/
</VirtualHost>

Comment: Nope; sadly, this doesn't work. I had a ServerAlias before, and I  tried it again just now; with the ServerAlias variant, the separate line is not present, and it *silently* won't work.
Answer: I just had a closer look, and I found the cause: the very ServerAlias www.mydomain.net was present in two virtual hosts (a right one, and the other).  Removing the wrong entry solved the problem.
Thus, Apache doesn't seem to care about such collisions; you can't rely on the virtual hosts dump running w/no abnormalities detected.


Detect Hardware Acceleration at Runtime: Android

Question: Is it possible to consistently detect if an Activity has hardware acceleration enabled when it is created? I'm worried that users of my library will enable it through the manifest when they shouldn't, by not specifically disabling it for my Activity (as I instruct them to do.)
The only solid information I can find (http://android-developers.blogspot.com/2011/03/android-30-hardware-acceleration.html) says that I can query View.isHardwareAccelerated() and Canvas.isHardwareAccelerated(). However, for my purposes, I would like to ensure it is off when my library's Activity is shown. So far, I can't get anything to report a consistent yes/no when it is on or off. I tried hacking in a dummy view, setting it to my activity and then testing it, but it always returns false. Also, I tried testing Window.getAttributes( ).flags, but they aren't showing it either.
I am testing this because the hardware accelerated draw path for my library doesn't function correctly, and there doesn't seem like there is any way to fix it.
Answer: Try FLAG_HARDWARE_ACCELERATED in flags in ActivityInfo for the activity, which you would get from PackageManager via getActivityInfo().
Comment: Another way of doing it is to call getDecorView().isHardwareAccelerated() from the Activity's onCreate() method.
Answer: I'm new in Android so I was stuck even with the clues given in the answer above.. went to search around and found this code somewhere in the sea of Google. Hope it helps someone.
/**
 * Returns true if the given Activity has hardware acceleration enabled
 * in its manifest, or in its foreground window.
 *
 * TODO(husky): Remove when initialize() is refactored (see TODO there)
 * TODO(dtrainor) This is still used by other classes.  Make sure to pull some version of this
 * out before removing it.
 */
public static boolean hasHardwareAcceleration(Activity activity) {
    // Has HW acceleration been enabled manually in the current window?
    Window window = activity.getWindow();
    if (window != null) {
        if ((window.getAttributes().flags
                & WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0) {
            return true;
        }
    }

    // Has HW acceleration been enabled in the manifest?
    try {
        ActivityInfo info = activity.getPackageManager().getActivityInfo(
                activity.getComponentName(), 0);
        if ((info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
            return true;
        }
    } catch (PackageManager.NameNotFoundException e) {
        Log.e("Chrome", "getActivityInfo(self) should not fail");
    }

    return false;
}

Comment: Hi bruce.. please answer to the following question also.. http://stackoverflow.com/questions/12481198/hardware-acceleration-supported-device-list


Using a computed field: How to extract a field value from a referenced node into field collection. (Multiple value)

Question: I have written this code inside a computed field, which has given me as result,
the referenced entity numbers how are saved in the field as an array.
I would like to know how to extract from the entity number a specific field, such as the title.
<code>foreach ($entity->field_campos as $field) {
}
foreach ($field as $index => $valores) {
$terminos[] = $valores['value'];
}
$item2 = field_collection_item_load_multiple($terminos, $reset = FALSE);
foreach ($item2 as $indice => $values) {
$entity_field[]['value'] = $values->field_nombre_2['und'][0]['target_id'];
}

// Display code (PHP).
$display_output = $entity_field_item['value'];
</code>
I am using Drupal 7.
Answer: You can do something like this:
<code>$referenced_titles = array();

foreach ($item2 as $indice => $values) {

  // use node_load($nid) to load the node.
  $referenced_node = node_load($values->field_nombre_2['und'][0]['target_id']);
  $referenced_titles[] = $referenced_node->title;

}
</code>


How to make Div over a body

Question: I'm going to develop an addon for Firefox, but I have a problem with the design. To explain the situation in the following picture you see a popup which has a darkgrey box in it. This box I animated from top to bottom, so it slow move in the popup. This is a div, and I want that the div is over the white body. So the div is animated in and everything behind is no more visible, but the div has the same width and same height as the body. You can still see the body right and left and on top and at the bottom, but why? How can I do it that the div is over the body design?
The picture: http://prntscr.com/puo44r
This is my code:
<!DOCTYPE html>

          <meta http-equiv="content-type" content="text/html; charset=utf-8" />

Answer: You can add position: fixed to div.
When element has position: fixed it is removed from flow of the page and it will be above every element of page.
Hope this helps.. Comment down if it doesn't :)

How to make Div over a body


CSS Google Font Url has my site's domain prefixed

Question: In my sass I have the following line:
@import url(http://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700|Droid+Serif:400,400italic,700);

If I paste that Url in the browser I can see that absolute Urls are defined for each font, such as :
src: local('Droid Serif'), local('DroidSerif'), url(http://fonts.gstatic.com/s/droidserif/v6/0AKsP294HTD-nvJgucYTaI4P5ICox8Kq3LLUNMylGO4.woff2) format('woff2');

But on the my page at http://misterfresh.github.io/react-drive-cms
you can see in the console that I have three errors for missing fonts, such as:
GET http://misterfresh.github.io/fonts.gstatic.com/s/sourcesanspro/v9/toadOcfmlt9b38dHJxOBGLsbIrGiHa6JIepkyt5c0A0.ttf

Comment: I don't know what this has to do with Sass.  There's not enough information here to reproduce a problem, since Sass does not even touch external CSS files and can't possibly do what you're claiming.
Comment: i think you should add a  tag in html page https://www.google.com/fonts#QuickUsePlace:quickUse
Comment: Cannot reproduce: http://sassmeister.com/gist/2a57eb4987a6c9194523
Comment: @cimmanon If I knew what it had to do with, I wouldn't be posting the question in the first place
Comment: My point here is that you haven't even tried examining where the problem is coming from.  You didn't look at the compiled CSS (that would have explained the 404).  You've probably got a postprocessor mangling your CSS that you didn't even bother mentioning.
Comment: @cimmanon I'm using gulp-sass with libsass to compile sass and gulp-minify-css to minify css. Yes I played around with the $icon-font-path variable to see if that helped, but then I decided to post a question, and thanks to LarsW a workaround was found.
Answer: In you CSS is now this as src:
url(../../fonts.gstatic.com/s/sourcesanspro/v9/toadOcfmlt9b38dHJxOBGLsbIrGiHa6JIepkyt5c0A0.ttf)

You can change it into (with or without quotes):
url(http://fonts.gstatic.com/s/sourcesanspro/v9/toadOcfmlt9b38dHJxOBGLsbIrGiHa6JIepkyt5c0A0.ttf)
Or just link the original thing.
Comment: I copied the contents from url(http://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700|Droid+Serif:400,400italic,700) directly towards the end of the css and it worked. still don't know why it failed with import


Finding a limit of a matrix raised to the $n$-th power

Question: There is a stochastic matrix given
$$\mathbb{P} = \begin{bmatrix} 0&\frac{1}{2}&\frac{1}{2}&0 \\ 0&1&0&0 \\ \frac{1}{2}&0&0&\frac{1}{2} \\ \frac{1}{3}&0&\frac{1}{3}&\frac{1}{3} \end{bmatrix}$$
I am to find $\mathbb{P}^n$. I know a theorem which says that if $\exists n$ $[p_{i,j}^n] >0$ then the limit exists and can be easily calculated (of course by $[p_{i,j}^n]$ I mean elements of $\mathbb{P}^n$).
In my case however $[p_{2,1}^n] = [p_{2,3}^n] = [p_{2,4}^n] =0, \forall n$. How can I solve this problem? Is there another theorem?
Answer: State $2$ is absorbing and the other three states are all transient (because they all lead to state $2$). Hence $\lim_{n \to \infty} p_{ij}^{n}=0$ if $j \neq 2$ and $\lim_{n \to \infty} p_{ij}^{n}=1$ if $j = 2$.
Comment: Look at the identity matrix.
Comment: Does it mean that if one state is absorbing then the limit will always look like the one that was written by you? (One columns of ones and the rest full of zeros )
Comment: For all j? But such a matrix wouldn't be stochastic then, would it?
Comment: Yes, of course! Thank you :)
Answer: We have $P=SDS^{-1}$, where
$$
S=\pmatrix{
-1 & -1 & \frac{9}{16} & 1 \\
0 & 0 & 0 & 1 \\
1 & 0 & \frac{15}{16} & 1 \\
0 & 1 & 1 & 1 \\
}
,\quad
D=\pmatrix{
-\frac{1}{2} & 0 & 0 & 0 \\
0 & 0 & 0 & 0 \\
0 & 0 & \frac{5}{6} & 0 \\
0 & 0 & 0 & 1 \\
}
$$
Therefore, $P^\infty = S D^\infty S^{-1}$.
Since
$$
D^\infty = \pmatrix{
0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 \\
0 & 0 & 0 & 1 \\
}
$$
we have
$$
P^\infty = \pmatrix{
0 & 1 & 0 & 0 \\
0 & 1 & 0 & 0 \\
0 & 1 & 0 & 0 \\
0 & 1 & 0 & 0 \\
}
$$


Checkboxes in exposed form in block aren't checked in initial state

Question: I have views with exposed form.
In filter criteria I have filter category which has 4 checkboxes.
If I choose to display exposed form in block, in initial state checkboxes aren't checked but results appears for each of them. It seems like a bug, user is not selected nothing but results are there. I want to these checkboxes be checked in initial state when the page is opened.
If exposed form isn't displayed in block, the checkboxes are checked in initial state.
I also try to change with form alter but without success, I couldn't print anything
<code>function module_form_alter(&$form, &$form_state, $form_id) {
    switch ($form_id) {
        case 'views_exposed_form':
            krumo($form);
        break;
    }
}
</code>
Answer: To do so you might have to checkboxes when you expose the form and save the view.


Como fazer atribuição de variáveis em massa no Python?

Question: No PHP, para fazermos uma atribuição de variáveis "em massa" (declarás-la na mesma linha), podemos fazer utilizando a função list.
Exemplo:
list($a, $b, $c) = array(1, 2, 3);

Como fazer esse tipo de atribuição em massa no Python?
Answer: É bem parecido:
(a, b, c) = 1, 2, 3

Comment: não precisa do parênteses do lado esquerdo. (Por outrolado, dependendo da expressão, pode ser uma boa ter parênteses do lado direito)
Comment: Com poucas palavras...
Answer: O conceito a que você se refere se chama unpacking.
Você pode utilizar um simples
a, b, c = 1, 2, 3

Observe que tal conceito também serve para realizar o unpacking de vários tipos de sequência diferente, sejam tuplas (tal como implicitamente é o seu exemplo) ou mesmo listas. De fato, como em Python tudo é um objeto, você pode gerar a lista dinamicamente por uma função:
a, b, c = range(1, 4)

Só observe que o número de váriáveis a serem atribuídas deve ser igual ao número de elementos na sequência, ou será levantado um ValueError:
>>> a, b, c = range(1, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack

Este conceito também é muito útil na hora de passar argumentos para funções:
>>> from __future__ import print_function
>>> print(range(3))
[0, 1, 2]
>>> print(*range(3))
0 1 2


How to create a zip archive of a directory?

Question: How can I create a zip archive of a directory structure in Python?
Comment: Don't use the solution suggested in the accepted answer but the one further down using `make_archive` from `shutil` (if you want to zip a single directory recursively).
Comment: The accepted answer is the only one that is actually thread safe in regards to read/write access while creating the zipfile from a directory since each file is opened individually, locking read access to it until the file is closed.
Comment: yes, agree with @malana - Martha Yi seems to be unregistered - so is there now way to change the accepted answer through a community process?
Comment: one caveat with `shutil.make_archive` - it doesn't seem to follow symlinks
Comment: @Beefcake It says in `make_archive`'s documentation that "_Changed in version 3.10.6_: This function is now made thread-safe during creation of standard `.zip` and tar archives."
Answer: The easiest way is to use shutil.make_archive. It supports both zip and tar formats.
import shutil
shutil.make_archive(output_filename, 'zip', dir_name)

If you need to do something more complicated than zipping the whole directory (such as skipping certain files), then you'll need to dig into the zipfile module as others have suggested.
Comment: `shutil` is part of the standard python library. This should be the top answer
Comment: Be warned that prior to Python 3.4, shutil.make_archive does not support ZIP64 and will fail on creating ZIP files larger than 2GB.
Comment: This is the most concise answer here and also has the advantage of adding all subdirectories and files to the archive directly, rather than having everything included in a top-level folder (which results in a redundant level in the folder structure when unzipping).
Comment: @Teekin No. If you look at the bugs report (bugs.python.org/issue30511), you'll see that `shutil.make_archive` uses `os.chdir()`. From what I'm reading about `os.chdir()`, it operates globally.
Comment: @cmcginty could you please be a bit more specific as to what aspect of it is not thread-safe? Will running multiple threads while one calls this cause the interpreter to crash?
Comment: This answer is not correct. It doesn't compress anything, plus is slower. the accepted answer is correct and should be the reference.
Comment: shutil is very, very slow. So, for it is not good option for dirs with large files.
Comment: @std''OrgnlDave No, it will not crash. The issue I've seen is that files from different threads will be copied into the same zip file. See https://bugs.python.org/issue30511
Comment: Even though it's not thread-safe, it should still be safe if being used by different threads in different directories, right?
Comment: @Jane See the accepted answer or one of the other answers on this page that uses the zipfile library instead of shutil.
Comment: If I want to zip a directory instead of the content of directory - is there an option for that? Aslo: can't find what's the equivalent of --remove-files?
Comment: @SwissNavy see `base_dir` option
Comment: I'm trying to automate zipping my work directory which is ~200MB as daily backup, this code kept working and made +84GB and threw an error because my work PC ran out of storage!
Comment: can we get to 2023?
Comment: @azdev, what is the solution for shutil.make_archive for files more than 2GB? I had this error(
Comment: @azdev For python version 3.6 to 3.7 does shutil work safely for files larger than 2GB?
Comment: Note this is NOT threadsafe! This does an explicit `os.chdir` which can lead to the wrong directory getting zipped. This led to my zips being gigantic and my disk running out of space.
Comment: for an actual example using both root_dir and base_dir, see: https://docs.python.org/3/library/shutil.html#shutil-archiving-example-with-basedir
Comment: This is not safe,this fails for directories with huge files.
Comment: Does anyone know how to get files referenced by a symlink to copy into the zip archive? When I unzip the created archive a directory for the symlink files is there but the directory is empty.
Comment: @xjcl Looks like this is thread-safe now for normal use. From the docs: `Changed in version 3.10.6: This function is now made thread-safe during creation of standard .zip and tar archives.`
Answer: As others have pointed out, you should use zipfile. The documentation tells you what functions are available, but doesn't really explain how you can use them to zip an entire directory. I think it's easiest to explain with some example code:
import os
import zipfile

def zipdir(path, ziph):
    # ziph is zipfile handle
    for root, dirs, files in os.walk(path):
        for file in files:
            ziph.write(os.path.join(root, file),
                       os.path.relpath(os.path.join(root, file),
                                       os.path.join(path, '..')))

with zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
    zipdir('tmp/', zipf)

Comment: I would add a second argument to the write call, passing `os.path.relpath(os.path.join(root, file), os.path.join(path, '..'))`. That would let you zip a directory from any working directory, without getting the full absolute paths in the archive.
Comment: `shutil` makes it really easy in just a single line. Please check the answer below..
Comment: There's a funny recursion going on when I try to zip a folder and output the resultant zip to the same folder. :-)
Comment: you may be more interested by doing ziph.write(os.path.join(path,file), arcname=file) so that the filenames inside the archive are not relative to the hard drive
Comment: The next answer does not handle anything related to compression, it just stores the results in a ZIP archive. If you are looking for actual compression, the correct answer is this one and not the next with `shutil`.
Comment: Ah, I was missing the `.close()` call!
Comment: May also be used with a context manager:

`with zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
    zipdir('tmp/', zipfh)`
Comment: This answer creates a zip containing the directory. How would you change it so that the _contents_ of the directory are added, rather than the directory itself?
Comment: I am getting "The compressed (zipped) folder is empty" on Windows 7
Comment: @RoachLord: `file` is not a keyword, even if your editor highlights it as one.
Comment: The next answer if more straightforward, and should have been accepted
Comment: What is ````ziph```` in this function? ````path```` is the directory. What if I have a directory containing other sub-directories? I added a question related to this. https://stackoverflow.com/questions/67119676/zip-file-with-different-directories-and-files-python?noredirect=1#comment118642041_67119676
Comment: I want to add that this option lets you make a zip archive with any file extension. i.e. `zipfile.ZipFile('Python.meta_zip', 'w')` At my work we have to group a metadata dir with files in it and the file they are associated with together. We don't need compression, we just want to easily have them all in one location when we upload them to the cloud.
Comment: Explaining with examples is the only way to explain properly, thanks :)
Comment: Using the `arcname` parameter of the write function will solve the issue where the entire directory branch is getting zipped rather than just the contents.
Comment: This will not write empty directories to the zip file. To fix this you can also write the `root` in every `os.walk` iteration.
Comment: @Prince and @Shaked do u have an example which accomplish these tasks i.e. writing empty directories and contents of the directory. because i am confused what should we assign to `arcname`
Comment: ok the second argument in `ziph.write` should be modified to `os.path.relpath(os.path.join(root, file), path)` to add the contents of the directory not the entire directory. the only thing remaining is how to write empty directories. @Shaked do u have an example which shows to add empy directories
Comment: @SajjadAli os.mkdir()
Answer: To add the contents of mydirectory to a new zip file, including all files and subdirectories:
import os
import zipfile

zf = zipfile.ZipFile("myzipfile.zip", "w")
for dirname, subdirs, files in os.walk("mydirectory"):
    zf.write(dirname)
    for filename in files:
        zf.write(os.path.join(dirname, filename))
zf.close()

Comment: Can't you use a `with` instead of having to call `close()` yourself at the end?
Comment: This rebuilds the full path to "mydirectory" within the resulting zipfile. i.e. only works as desired if "mydirectory" is in root of your filesystem
Comment: For me this code throwing below error    TypeError: invalid file:
Comment: example:

`
with zipfile.ZipFile("myzipfile.zip", "w") as zf: pass
`
Comment: Using the `arcname` parameter of the write function will solve the issue where the entire directory branch is getting zipped rather than just the contents.
Answer:
How can I create a zip archive of a directory structure in Python?

In a Python script
In Python 2.7+, shutil has a make_archive function.
from shutil import make_archive
make_archive(
  'zipfile_name',
  'zip',           # the archive format - or tar, bztar, gztar
  root_dir=None,   # root for archive - current working dir if None
  base_dir=None)   # start archiving from here - cwd if None too

Here the zipped archive will be named zipfile_name.zip. If base_dir is farther down from root_dir it will exclude files not in the base_dir, but still archive the files in the parent dirs up to the root_dir.
I did have an issue testing this on Cygwin with 2.7 - it wants a root_dir argument, for cwd:
make_archive('zipfile_name', 'zip', root_dir='.')

Using Python from the shell
You can do this with Python from the shell also using the zipfile module:
$ python -m zipfile -c zipname sourcedir

Where zipname is the name of the destination file you want (add .zip if you want it, it won't do it automatically) and sourcedir is the path to the directory.
Zipping up Python (or just don't want parent dir):
If you're trying to zip up a python package with a __init__.py and __main__.py, and you don't want the parent dir, it's
$ python -m zipfile -c zipname sourcedir/*

And
$ python zipname

would run the package. (Note that you can't run subpackages as the entry point from a zipped archive.)
Zipping a Python app:
If you have python3.5+, and specifically want to zip up a Python package, use zipapp:
$ python -m zipapp myapp
$ python myapp.pyz

Answer: This function will recursively zip up a directory tree, compressing the files, and recording the correct relative filenames in the archive. The archive entries are the same as those generated by zip -r output.zip source_dir.
import os
import zipfile
def make_zipfile(output_filename, source_dir):
    relroot = os.path.abspath(os.path.join(source_dir, os.pardir))
    with zipfile.ZipFile(output_filename, "w", zipfile.ZIP_DEFLATED) as zip:
        for root, dirs, files in os.walk(source_dir):
            # add directory (needed for empty dirs)
            zip.write(root, os.path.relpath(root, relroot))
            for file in files:
                filename = os.path.join(root, file)
                if os.path.isfile(filename): # regular files only
                    arcname = os.path.join(os.path.relpath(root, relroot), file)
                    zip.write(filename, arcname)

Comment: Sweet, I was wondering if `zipfile` could be used in a `with` statement.
Thanks for pointing out it can.
Answer: With python 3.9, pathlib & zipfile module you can create a zip files from anywhere in the system.
def zip_dir(dir: Union[Path, str], filename: Union[Path, str]):
    """Zip the provided directory without navigating to that directory using `pathlib` module"""

    # Convert to Path object
    dir = Path(dir)

    with zipfile.ZipFile(filename, "w", zipfile.ZIP_DEFLATED) as zip_file:
        for entry in dir.rglob("*"):
            zip_file.write(entry, entry.relative_to(dir))

It is neat, typed, and has less code.
Answer: Modern Python (3.6+) using the pathlib module for concise OOP-like handling of paths, and pathlib.Path.rglob() for recursive globbing. As far as I can tell, this is equivalent to George V. Reilly's answer: zips with compression, the topmost element is a directory, keeps empty dirs, uses relative paths.
from pathlib import Path
from zipfile import ZIP_DEFLATED, ZipFile

from os import PathLike
from typing import Union

def zip_dir(zip_name: str, source_dir: Union[str, PathLike]):
    src_path = Path(source_dir).expanduser().resolve(strict=True)
    with ZipFile(zip_name, 'w', ZIP_DEFLATED) as zf:
        for file in src_path.rglob('*'):
            zf.write(file, file.relative_to(src_path.parent))

Note: as optional type hints indicate, zip_name can't be a Path object (would be fixed in 3.6.2+).
Answer: Use shutil, which is part of python standard library set.
Using shutil is so simple(see code below):

1st arg: Filename of resultant zip/tar file,
2nd arg: zip/tar,
3rd arg: dir_name

Code:
import shutil
shutil.make_archive('/home/user/Desktop/Filename','zip','/home/username/Desktop/Directory')

Comment: With all the shutil.make_archvie examples here, they all create empty root folders leading up to the folder that you actually want to archive. I do not want my archive file unarchive with "/home/user/Desktop" so everyone can see where folder of interest was originally. How do i just zip "/Directory" and leave out all traces of parent folders?
Comment: This has been said 3 times already. And it's definitely not the best answer.
Answer: For adding compression to the resulting zip file, check out this link.
You need to change:
zip = zipfile.ZipFile('Python.zip', 'w')

to
zip = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)

Answer: I've made some changes to code given by Mark Byers. Below function will also adds empty directories if you have them. Examples should make it more clear what is the path added to the zip.
#!/usr/bin/env python
import os
import zipfile

def addDirToZip(zipHandle, path, basePath=""):
    """
    Adding directory given by \a path to opened zip file \a zipHandle

    @param basePath path that will be removed from \a path when adding to archive

    Examples:
        # add whole "dir" to "test.zip" (when you open "test.zip" you will see only "dir")
        zipHandle = zipfile.ZipFile('test.zip', 'w')
        addDirToZip(zipHandle, 'dir')
        zipHandle.close()

        # add contents of "dir" to "test.zip" (when you open "test.zip" you will see only it's contents)
        zipHandle = zipfile.ZipFile('test.zip', 'w')
        addDirToZip(zipHandle, 'dir', 'dir')
        zipHandle.close()

        # add contents of "dir/subdir" to "test.zip" (when you open "test.zip" you will see only contents of "subdir")
        zipHandle = zipfile.ZipFile('test.zip', 'w')
        addDirToZip(zipHandle, 'dir/subdir', 'dir/subdir')
        zipHandle.close()

        # add whole "dir/subdir" to "test.zip" (when you open "test.zip" you will see only "subdir")
        zipHandle = zipfile.ZipFile('test.zip', 'w')
        addDirToZip(zipHandle, 'dir/subdir', 'dir')
        zipHandle.close()

        # add whole "dir/subdir" with full path to "test.zip" (when you open "test.zip" you will see only "dir" and inside it only "subdir")
        zipHandle = zipfile.ZipFile('test.zip', 'w')
        addDirToZip(zipHandle, 'dir/subdir')
        zipHandle.close()

        # add whole "dir" and "otherDir" (with full path) to "test.zip" (when you open "test.zip" you will see only "dir" and "otherDir")
        zipHandle = zipfile.ZipFile('test.zip', 'w')
        addDirToZip(zipHandle, 'dir')
        addDirToZip(zipHandle, 'otherDir')
        zipHandle.close()
    """
    basePath = basePath.rstrip("\\/") + ""
    basePath = basePath.rstrip("\\/")
    for root, dirs, files in os.walk(path):
        # add dir itself (needed for empty dirs
        zipHandle.write(os.path.join(root, "."))
        # add files
        for file in files:
            filePath = os.path.join(root, file)
            inZipPath = filePath.replace(basePath, "", 1).lstrip("\\/")
            #print filePath + " , " + inZipPath
            zipHandle.write(filePath, inZipPath)

Above is a simple function that should work for simple cases. You can find more elegant class in my Gist:
https://gist.github.com/Eccenux/17526123107ca0ac28e6
Comment: The path handling could be greatly simplified by using [os.path](http://doughellmann.com/2008/01/pymotw-os-path.html). See my answer.
Comment: Bug: zipHandle.write(os.path.join(root, ".")) does not take basePath into consideration.
Comment: Yes, you're probably right. I've later enhnaced this a bit ;-)
https://gist.github.com/Eccenux/17526123107ca0ac28e6
Answer: To retain the folder hierarchy under the parent directory to be archived:
from pathlib import Path
import zipfile

fp_zip = Path("output.zip")
path_to_archive = Path("./path-to-archive")

with zipfile.ZipFile(fp_zip, "w", zipfile.ZIP_DEFLATED) as zipf:
    for fp in path_to_archive.glob("**/*"):
        zipf.write(fp, arcname=fp.relative_to(path_to_archive))

Answer: I have another code example that may help, using python3, pathlib and zipfile.
It should work in any OS.
from pathlib import Path
import zipfile
from datetime import datetime

DATE_FORMAT = '%y%m%d'

def date_str():
    """returns the today string year, month, day"""
    return '{}'.format(datetime.now().strftime(DATE_FORMAT))

def zip_name(path):
    """returns the zip filename as string"""
    cur_dir = Path(path).resolve()
    parent_dir = cur_dir.parents[0]
    zip_filename = '{}/{}_{}.zip'.format(parent_dir, cur_dir.name, date_str())
    p_zip = Path(zip_filename)
    n = 1
    while p_zip.exists():
        zip_filename = ('{}/{}_{}_{}.zip'.format(parent_dir, cur_dir.name,
                                             date_str(), n))
        p_zip = Path(zip_filename)
        n += 1
    return zip_filename

def all_files(path):
    """iterator returns all files and folders from path as absolute path string
    """
    for child in Path(path).iterdir():
        yield str(child)
        if child.is_dir():
            for grand_child in all_files(str(child)):
                yield str(Path(grand_child))

def zip_dir(path):
    """generate a zip"""
    zip_filename = zip_name(path)
    zip_file = zipfile.ZipFile(zip_filename, 'w')
    print('create:', zip_filename)
    for file in all_files(path):
        print('adding... ', file)
        zip_file.write(file)
    zip_file.close()

if __name__ == '__main__':
    zip_dir('.')
    print('end!')

Answer: If you want a functionality like the compress folder of any common graphical file manager you can use the following code, it uses the zipfile module. Using this code you will have the zip file with the path as its root folder.
import os
import zipfile

def zipdir(path, ziph):
    # Iterate all the directories and files
    for root, dirs, files in os.walk(path):
        # Create a prefix variable with the folder structure inside the path folder.
        # So if a file is at the path directory will be at the root directory of the zip file
        # so the prefix will be empty. If the file belongs to a containing folder of path folder
        # then the prefix will be that folder.
        if root.replace(path,'') == '':
                prefix = ''
        else:
                # Keep the folder structure after the path folder, append a '/' at the end
                # and remome the first character, if it is a '/' in order to have a path like
                # folder1/folder2/file.txt
                prefix = root.replace(path, '') + '/'
                if (prefix[0] == '/'):
                        prefix = prefix[1:]
        for filename in files:
                actual_file_path = root + '/' + filename
                zipped_file_path = prefix + filename
                zipf.write( actual_file_path, zipped_file_path)

zipf = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir('/tmp/justtest/', zipf)
zipf.close()

Answer: So many answers here, and I hope I might contribute with my own version, which is based on the original answer (by the way), but with a more graphical perspective, also using context for each zipfile setup and sorting os.walk(), in order to have a ordered output.
Having these folders and them files (among other folders), I wanted to create a .zip for each cap_ folder:
$ tree -d
.
├── cap_01
|    ├── 0101000001.json
|    ├── 0101000002.json
|    ├── 0101000003.json
|
├── cap_02
|    ├── 0201000001.json
|    ├── 0201000002.json
|    ├── 0201001003.json
|
├── cap_03
|    ├── 0301000001.json
|    ├── 0301000002.json
|    ├── 0301000003.json
|
├── docs
|    ├── map.txt
|    ├── main_data.xml
|
├── core_files
     ├── core_master
     ├── core_slave

Here's what I applied, with comments for better understanding of the process.
$ cat zip_cap_dirs.py
""" Zip 'cap_*' directories. """
import os
import zipfile as zf

for root, dirs, files in sorted(os.walk('.')):
    if 'cap_' in root:
        print(f"Compressing: {root}")
        # Defining .zip name, according to Capítulo.
        cap_dir_zip = '{}.zip'.format(root)
        # Opening zipfile context for current root dir.
        with zf.ZipFile(cap_dir_zip, 'w', zf.ZIP_DEFLATED) as new_zip:
            # Iterating over os.walk list of files for the current root dir.
            for f in files:
                # Defining relative path to files from current root dir.
                f_path = os.path.join(root, f)
                # Writing the file on the .zip file of the context
                new_zip.write(f_path)

Basically, for each iteration over os.walk(path), I'm opening a context for zipfile setup and afterwards, iterating iterating over files, which is a list of files from root directory, forming the relative path for each file based on the current root directory, appending to the zipfile context which is running.
And the output is presented like this:
$ python3 zip_cap_dirs.py
Compressing: ./cap_01
Compressing: ./cap_02
Compressing: ./cap_03

To see the contents of each .zip directory, you can use less command:
$ less cap_01.zip

Archive:  cap_01.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
  22017  Defl:N     2471  89% 2019-09-05 08:05 7a3b5ec6  cap_01/0101000001.json
  21998  Defl:N     2471  89% 2019-09-05 08:05 155bece7  cap_01/0101000002.json
  23236  Defl:N     2573  89% 2019-09-05 08:05 55fced20  cap_01/0101000003.json
--------          ------- ---                           -------
  67251             7515  89%                            3 files

Answer: A solution using pathlib.Path, which is independent of the OS used:
import zipfile
from pathlib import Path

def zip_dir(path: Path, zip_file_path: Path):
    """Zip all contents of path to zip_file"""
    files_to_zip = [
        file for file in path.glob('*') if file.is_file()]
    with zipfile.ZipFile(
        zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zip_f:
        for file in files_to_zip:
            print(file.name)
            zip_f.write(file, file.name)

current_dir = Path.cwd()
tozip_dir = current_dir / "test"
zip_dir(
    tozip_dir, current_dir / 'dir.zip')

Answer: To give more flexibility, e.g. select directory/file by name use:
import os
import zipfile

def zipall(ob, path, rel=""):
    basename = os.path.basename(path)
    if os.path.isdir(path):
        if rel == "":
            rel = basename
        ob.write(path, os.path.join(rel))
        for root, dirs, files in os.walk(path):
            for d in dirs:
                zipall(ob, os.path.join(root, d), os.path.join(rel, d))
            for f in files:
                ob.write(os.path.join(root, f), os.path.join(rel, f))
            break
    elif os.path.isfile(path):
        ob.write(path, os.path.join(rel, basename))
    else:
        pass

For a file tree:
.
├── dir
│   ├── dir2
│   │   └── file2.txt
│   ├── dir3
│   │   └── file3.txt
│   └── file.txt
├── dir4
│   ├── dir5
│   └── file4.txt
├── listdir.zip
├── main.py
├── root.txt
└── selective.zip

You can e.g. select only dir4 and root.txt:
cwd = os.getcwd()
files = [os.path.join(cwd, f) for f in ['dir4', 'root.txt']]

with zipfile.ZipFile("selective.zip", "w" ) as myzip:
    for f in files:
        zipall(myzip, f)

Or just listdir in script invocation directory and add everything from there:
with zipfile.ZipFile("listdir.zip", "w" ) as myzip:
    for f in os.listdir():
        if f == "listdir.zip":
            # Creating a listdir.zip in the same directory
            # will include listdir.zip inside itself, beware of this
            continue
        zipall(myzip, f)

Comment: This zips, but doesn't compress.
Answer: While using shutil note that you should include output directory path in base_name arg:
import shutil

shutil.make_archive(
  base_name=output_dir_path + output_filename_without_extension,
  format="zip",
  root_dir=input_root_dir)

Answer: You probably want to look at the zipfile module; there's documentation at http://docs.python.org/library/zipfile.html.
You may also want os.walk() to index the directory structure.
Answer: Here is a variation on the answer given by Nux that works for me:
def WriteDirectoryToZipFile( zipHandle, srcPath, zipLocalPath = "", zipOperation = zipfile.ZIP_DEFLATED ):
    basePath = os.path.split( srcPath )[ 0 ]
    for root, dirs, files in os.walk( srcPath ):
        p = os.path.join( zipLocalPath, root [ ( len( basePath ) + 1 ) : ] )
        # add dir
        zipHandle.write( root, p, zipOperation )
        # add files
        for f in files:
            filePath = os.path.join( root, f )
            fileInZipPath = os.path.join( p, f )
            zipHandle.write( filePath, fileInZipPath, zipOperation )

Answer: Try the below one .it worked for me.
import zipfile, os
zipf = "compress.zip"
def main():
    directory = r"Filepath"
    toZip(directory)
def toZip(directory):
    zippedHelp = zipfile.ZipFile(zipf, "w", compression=zipfile.ZIP_DEFLATED )

    list = os.listdir(directory)
    for file_list in list:
        file_name = os.path.join(directory,file_list)

        if os.path.isfile(file_name):
            print file_name
            zippedHelp.write(file_name)
        else:
            addFolderToZip(zippedHelp,file_list,directory)
            print "---------------Directory Found-----------------------"
    zippedHelp.close()

def addFolderToZip(zippedHelp,folder,directory):
    path=os.path.join(directory,folder)
    print path
    file_list=os.listdir(path)
    for file_name in file_list:
        file_path=os.path.join(path,file_name)
        if os.path.isfile(file_path):
            zippedHelp.write(file_path)
        elif os.path.isdir(file_name):
            print "------------------sub directory found--------------------"
            addFolderToZip(zippedHelp,file_name,path)

if __name__=="__main__":
    main()

Answer: Say you want to Zip all the folders(sub directories) in the current directory.
for root, dirs, files in os.walk("."):
    for sub_dir in dirs:
        zip_you_want = sub_dir+".zip"
        zip_process = zipfile.ZipFile(zip_you_want, "w", zipfile.ZIP_DEFLATED)
        zip_process.write(file_you_want_to_include)
        zip_process.close()

        print("Successfully zipped directory: {sub_dir}".format(sub_dir=sub_dir))

Answer: Zip a file or a tree (a directory and its sub-directories).
from pathlib import Path
from zipfile import ZipFile, ZIP_DEFLATED

def make_zip(tree_path, zip_path, mode='w', skip_empty_dir=False):
    with ZipFile(zip_path, mode=mode, compression=ZIP_DEFLATED) as zf:
        paths = [Path(tree_path)]
        while paths:
            p = paths.pop()
            if p.is_dir():
                paths.extend(p.iterdir())
                if skip_empty_dir:
                    continue
            zf.write(p)

To append to an existing archive, pass mode='a', to create a fresh archive mode='w' (the default in the above). So let's say you want to bundle 3 different directory trees under the same archive.
make_zip(path_to_tree1, path_to_arch, mode='w')
make_zip(path_to_tree2, path_to_arch, mode='a')
make_zip(path_to_file3, path_to_arch, mode='a')

Answer: The obvious way to go would be to go with shutil, Like the second top answer says so, But if you still wish to go with ZipFile for some reason, And if you are getting some trouble doing that (Like ERR 13 in Windows etc), You can use this fix:
import os
import zipfile

def retrieve_file_paths(dirName):
  filePaths = []
  for root, directories, files in os.walk(dirName):
    for filename in files:
        filePath = os.path.join(root, filename)
        filePaths.append(filePath)
  return filePaths

def main(dir_name, output_filename):
  filePaths = retrieve_file_paths(dir_name)

  zip_file = zipfile.ZipFile(output_filename+'.zip', 'w')
  with zip_file:
    for file in filePaths:
      zip_file.write(file)

main("my_dir", "my_dir_archived")

This one recursively iterates through every sub-folder/file in your given folder, And writes them to a zip file instead of attempting to directly zip a folder.
Answer: One thing completely missed by previous answers is the fact that using os.path.join() can easily return POSIX-incompatible paths when you run the code on Windows. The resulting archive will contain files with literal backslashes in their names when processing it with any common archive software on Linux, which is not what you want. Use path.as_posix() for the arcname parameter instead!
import zipfile
from pathlib import Path
with zipfile.ZipFile("archive.zip", "w", zipfile.ZIP_DEFLATED) as zf:
    for path in Path("include_all_of_this_folder").rglob("*"):
        zf.write(path, path.as_posix())

Answer: Here's a modern approach, using pathlib, and a context manager. Puts the files directly in the zip, rather than in a subfolder.
def zip_dir(filename: str, dir_to_zip: pathlib.Path):
    with zipfile.ZipFile(filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
        # Use glob instead of iterdir(), to cover all subdirectories.
        for directory in dir_to_zip.glob('**'):
            for file in directory.iterdir():
                if not file.is_file():
                    continue
                # Strip the first component, so we don't create an uneeded subdirectory
                # containing everything.
                zip_path = pathlib.Path(*file.parts[1:])
                # Use a string, since zipfile doesn't support pathlib  directly.
                zipf.write(str(file), str(zip_path))

Answer: I prepared a function by consolidating Mark Byers' solution with Reimund and Morten Zilmer's comments (relative path and including empty directories). As a best practice, with is used in ZipFile's file construction.
The function also prepares a default zip file name with the zipped directory name and '.zip' extension. Therefore, it works with only one argument: the source directory to be zipped.
import os
import zipfile

def zip_dir(path_dir, path_file_zip=''):
if not path_file_zip:
    path_file_zip = os.path.join(
        os.path.dirname(path_dir), os.path.basename(path_dir)+'.zip')
with zipfile.ZipFile(path_file_zip, 'wb', zipfile.ZIP_DEFLATED) as zip_file:
    for root, dirs, files in os.walk(path_dir):
        for file_or_dir in files + dirs:
            zip_file.write(
                os.path.join(root, file_or_dir),
                os.path.relpath(os.path.join(root, file_or_dir),
                                os.path.join(path_dir, os.path.pardir)))

Answer: # import required python modules
# You have to install zipfile package using pip install

import os,zipfile

# Change the directory where you want your new zip file to be

os.chdir('Type your destination')

# Create a new zipfile ( I called it myfile )

zf = zipfile.ZipFile('myfile.zip','w')

# os.walk gives a directory tree. Access the files using a for loop

for dirnames,folders,files in os.walk('Type your directory'):
    zf.write('Type your Directory')
    for file in files:
        zf.write(os.path.join('Type your directory',file))

Answer: Well, after reading the suggestions I came up with a very similar way that works with 2.7.x without creating "funny" directory names (absolute-like names), and will only create the specified folder inside the zip.
Or just in case you needed your zip to contain a folder inside with the contents of the selected directory.
def zipDir( path, ziph ) :
 """
 Inserts directory (path) into zipfile instance (ziph)
 """
 for root, dirs, files in os.walk( path ) :
  for file in files :
   ziph.write( os.path.join( root, file ) , os.path.basename( os.path.normpath( path ) ) + "\\" + file )

def makeZip( pathToFolder ) :
 """
 Creates a zip file with the specified folder
 """
 zipf = zipfile.ZipFile( pathToFolder + 'file.zip', 'w', zipfile.ZIP_DEFLATED )
 zipDir( pathToFolder, zipf )
 zipf.close()
 print( "Zip file saved to: " + pathToFolder)

makeZip( "c:\\path\\to\\folder\\to\\insert\\into\\zipfile" )

Answer: Function to create zip file.
def CREATEZIPFILE(zipname, path):
    #function to create a zip file
    #Parameters: zipname - name of the zip file; path - name of folder/file to be put in zip file

    zipf = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED)
    zipf.setpassword(b"password") #if you want to set password to zipfile

    #checks if the path is file or directory
    if os.path.isdir(path):
        for files in os.listdir(path):
            zipf.write(os.path.join(path, files), files)

    elif os.path.isfile(path):
        zipf.write(os.path.join(path), path)
    zipf.close()

Comment: please explain with an example so that i can correct my answer
Comment: However, zipfile "currently cannot create an encrypted file" (from https://docs.python.org/3.9/library/zipfile.html)
Answer: summary others, make a function:
import zipfile

def zipFolder(toZipFolder, outputZipFile):
  """
    zip/compress a whole folder/directory to zip file
  """
  print("Zip for foler %s" % toZipFolder)
  with zipfile.ZipFile(outputZipFile, 'w', zipfile.ZIP_DEFLATED) as zipFp:
    for dirpath, dirnames, filenames in os.walk(toZipFolder):
      # print("%s" % ("-"*80))
      # print("dirpath=%s, dirnames=%s, filenames=%s" % (dirpath, dirnames, filenames))
      # print("Folder: %s, Files: %s" % (dirpath, filenames))
      for curFileName in filenames:
        # print("curFileName=%s" % curFileName)
        curFilePath = os.path.join(dirpath, curFileName)
        # print("curFilePath=%s" % curFilePath)
        fileRelativePath = os.path.relpath(curFilePath, toZipFolder)
        # print("fileRelativePath=%s" % fileRelativePath)
        # print("  %s" % fileRelativePath)
        zipFp.write(curFilePath, arcname=fileRelativePath)
  print("Completed zip file %s" % outputZipFile)

call:
  toZipFullPath = "/Users/crifan/dev/dev_root/iosReverse/WhatsApp/ipa/forRepackIpa_20231128"
  outputZipFile = "/Users/crifan/dev/dev_root/iosReverse/WhatsApp/ipa/WhatsApp_v23.24.0_20231128.ipa"
  zipFolder(toZipFullPath, outputZipFile)

output:
Zip for foler /Users/crifan/dev/dev_root/iosReverse/WhatsApp/ipa/forRepackIpa_20231128
Completed zip file /Users/crifan/dev/dev_root/iosReverse/WhatsApp/ipa/WhatsApp_v23.20.79_idaAllSymbols_20231128.ipa

latest code: crifanLib/crifanFile.py


Prioridade em uma notificação usando firebase

Question: Estou utilizando ionic e tudo está configurado para mandar a notificação...
Queria aumentar a importancia dela porque ela não faz o barulho e nem aparece no canto superior como uma notificação padrão(como a de WhatsApp)
Como eu posso fazer para aumentar a importancia da notificação usando a API do Firebase Cloud Messaging
Answer: Imagino que você esteja usando o plugin do Ionic de FCM.
Na página da documentação, há um exemplo utilizando o campo "priority":"high":
<code>{
  "notification":{
    "title":"Notification title",
    "body":"Notification body",
    "sound":"default",
    "click_action":"FCM_PLUGIN_ACTIVITY",
    "icon":"fcm_push_icon"
  },
  "data":{
    "param1":"value1",
    "param2":"value2"
  },
    "to":"/topics/topicExample",
    "priority":"high",
    "restricted_package_name":""
}
</code>
Para utilizar, basta usar alguma ferramenta que faça post, como o Postman.
Comment: Na verdade não, estou usando o push notifications do phone-gap
Comment: nesse caso a propriedade se chama "importance". https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/API.md#channel-properties


Calculate x2 if we know distance, x1, y1, y2

Question: How can I calculate x1 in Java if I know the distance, x2, y1 and y2 of two points.
I made a picture to be easier to understand:

If we know the x1, x2, y1, y2 is easy to calculate the distance. But if we know the distance? How can I calculate with delta?
Comment: Please show us what have tried
Comment: Exactly as u calculate it on a paper.
Answer: Note that there are usually two solutions to this; corresponding to essentially what is a 'reflected' solution.
if d is the distance, then x2 is at
x1 +/- sqrt(d * d - (y2 - y1) * (y2 - y1));
Where +/- means "plus or minus". That is to repeat, x1 can be in two different places.
Comment: @duffymo: that's not true given that `x1` is known. Refer to the helpful diagram in the question: you get two points (which, in general can be coincident).
Comment: Missed that detail.  Reading too fast, too early in the morning.
Answer: d^2 = (x2-x1)^2 + (y2-y1)^2

(x2-x1)^2 = d^2 - (y2-y1)^2

x2-x1 = +- sqrt(d^2 - (y2-y1)^2)

x2 = x1 +- sqrt(d^2 - (y2-y1)^2)

Comment: More precisely, it gives two solutions out of two.
Answer: If you know the distance, you can use it's formula:
d = sqrt(sqr(x2 - x1) + sqr(y2 - y1))

With that, you can re-arrange it to find x1 using simple algebra, but I'll let you figure that out.
Answer: First, you need to solve for x2:
d = sqrt((x1-x2)^2+(y1-y2)^2)
d^2 = (x1-x2)^2+(y1-y2)^2
d^2-(y1-y2)^2 = (x1-x2)^2
sqrt(d^2-(y1-y2)^2) = (x1-x2)
x2 = x1-sqrt(d^2-(y1-y2)^2)

Now, it's just a matter of expressing this in java:
double x2 = x1 - Math.sqrt(Math.pow(d, 2) - Math.pow(y1 - y2, 2));

Comment: @Clashsoft: ostensibly that's correct (and I would advise against using `pow` for such small indices), but these days, compilers etc. optimise out the edge cases. Sadly this solution is only partially correct as the answer has dropped one of the roots. The principle is correct though.
Comment: Using `Math.pow(d, 2)` is a really arbitrary and inefficient way to do it, considering that `d * d` is easier to read anyway.
Comment: That's still less readable, and some people might not know what `pow` means in this case. Also, I know javac does Maths optimizations, but certainly not for `Math.pow(x, 2)`
Comment: @Clashsoft for me, readability trumps performance, unless there's a killer reason. `Math.pow` seems clearer to me, but it's all in the eye of the beholder. I'm on the fence wrt `d*d` vs. `Math.pow(d, 2)`, but for more "complex" expression, I'd definitely prefer `Math.pow(y1-y2,2)` over `(y1-y2)*(y1-y2)`.


norm of element in equivalent class in quotient space

Question: If we have a quotient space $E\backslash L_0$ where $E$ is a linear normed space and $L_0$ it's subspace the norm of an element $L$ in $E\backslash L_0$ is defined as $$\lVert L\rVert = \inf_{x \in L}{\lVert x\rVert}$$ What I'm trying to understand better is why we can find an element $x\in L$ s.t. $\lVert x \rVert < \lVert L \rVert + \epsilon$. I understand that such element is not going to be an infimum, but how are we sure that this element is going to be in $L$? Does is have something to do with the fact that $L$ is a closed set?
Thanks in advance!
Answer: This is immediate from definition of infimum. If this is not true then $\|x\| \geq \|L\|+\epsilon$ for all $x \in L$ which means $\|L\|+\epsilon$ is  a lower bound; hence the greatest lower bound $\|L\|$ must be equal to or  exceed this, i.e. $\|L\| \geq \|L\|+\epsilon$ which is a contardiction.
Comment: Ask yourself what happens if there is no such $x$. You will get a contradiction. That is what I have done in my answer.
Comment: I get that it's coming from the definition of an infimum. What confuses me is that we can always find an element $x \in L$ satisfying every $\epsilon > 0$. It's like there are no gaps between the elements in $L$.


No makefile created for opencv_contrib

Question: I'm trying to add the opencv_contrib package to my OpenCV 3.1 build. I am currently using the library in Microsoft Visual Studio 15. I have installed Cmake and GNU Make, cloned the opencv_contrib repo, and followed the instructions on https://github.com/opencv/opencv_contrib for building the package. The instructions are:
C:\Opencv310\build>cmake -DOPENCV_EXTRA_MODULES_PATH=C:\opencv_contrib\modules C:\Opencv310/sources
C:\Opencv310\build>make -j5

The first line runs fine and finishes with the couts:
--Configuring done
--Generating done
--Build files have been written to: C:Opencv310\build

When I run the final line i get the error:
make: *** No targets specified and no makefile found. Stop.

Has anyone of you guys ever encountered this error before while adding opencv_contrib? Do you see any errors in what I have done? Any help would be appreciated!
The full output from the first call is:
C:\Opencv310\build>cmake -DOPENCV_EXTRA_MODULES_PATH=C:\opencv_contrib\modules C:\Opencv310\sources
-- found IPP (ICV version): 9.0.1 [9.0.1]
-- at: C:/Opencv310/sources/3rdparty/ippicv/unpack/ippicv_win
-- Could NOT find Doxygen (missing:  DOXYGEN_EXECUTABLE)
-- To enable PlantUML support, set PLANTUML_JAR environment variable or pass          -DPLANTUML_JAR=<filepath> option to cmake
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named numpy.distutils
-- Could NOT find PythonInterp: Found unsuitable version "2.7.12", but     required is at least "3.4" (found C:/Python27/python.exe)
-- Could NOT find PythonInterp: Found unsuitable version "2.7.12", but     required is at least "3.2" (found C:/Python27/python.exe)
-- Could NOT find JNI (missing:  JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY     JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH)
-- Could NOT find Matlab (missing:  MATLAB_MEX_SCRIPT MATLAB_INCLUDE_DIRS     MATLAB_ROOT_DIR MATLAB_LIBRARIES MATLAB_LIBRARY_DIRS MATLAB_MEXEXT MATLAB_ARCH     MATLAB_BIN)
-- VTK is not found. Please set -DVTK_DIR in CMake to VTK build directory, or to VTK install subdirectory with VTKConfig.cmake file
-- Caffe:   NO
-- Protobuf:   NO
-- Glog:   NO
-- Module opencv_sfm disabled because the following dependencies are not     found: Eigen Glog/Gflags
-- Could NOT find Protobuf (missing:  Protobuf_LIBRARIES Protobuf_INCLUDE_DIR)
-- Build libprotobuf from sources:
--     libprotobuf not found into system
--     The protocol buffer compiler not found
-- Tesseract:   NO
-- Check contents of vgg_generated_48.i ...
-- Check contents of vgg_generated_64.i ...
-- Check contents of vgg_generated_80.i ...
-- Check contents of vgg_generated_120.i ...
-- Check contents of boostdesc_bgm.i ...
-- Check contents of boostdesc_bgm_bi.i ...
-- Check contents of boostdesc_bgm_hd.i ...
-- Check contents of boostdesc_binboost_064.i ...
-- Check contents of boostdesc_binboost_128.i ...
-- Check contents of boostdesc_binboost_256.i ...
-- Check contents of boostdesc_lbgm.i ...
--
-- General configuration for OpenCV 3.1.0     =====================================
--   Version control:               unknown
--
--   Platform:
--     Host:                        Windows 10.0.14393 AMD64
--     CMake:                       3.7.0-rc2
--     CMake generator:             Visual Studio 14 2015
--     CMake build tool:            C:/Program Files     (x86)/MSBuild/14.0/bin/MSBuild.exe
--     MSVC:                        1900
--
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++ Compiler:                C:/Program Files (x86)/Microsoft Visual     Studio 14.0/VC/bin/cl.exe  (ver 19.0.24210.0)
--     C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR /EHa  /D     _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS     /Gy /bigobj /arch:SSE2 /Oi /fp:fast  /wd4251 /wd4324 /wd4275 /wd4589 /MP4  /MD    /O2 /Ob2 /DNDEBUG  /Zi
--     C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR /EHa  /D     _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS     /Gy /bigobj /arch:SSE2 /Oi /fp:fast  /wd4251 /wd4324 /wd4275 /wd4589 /MP4      /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1
--     C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe
--     C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /arch:SSE2 /Oi /fp:fast    /MP4  /MD /O2 /Ob2 /DNDEBUG  /Zi
--     C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D   _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS  /Gy /bigobj /arch:SSE2 /Oi /fp:fast    /MP4  /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1
--     Linker flags (Release):      /machine:X86  /INCREMENTAL:NO  /debug
--     Linker flags (Debug):        /machine:X86  /debug /INCREMENTAL
--     Precompiled headers:         YES
--     Extra dependencies:          comctl32 gdi32 ole32 setupapi ws2_32 vfw32
--     3rdparty dependencies:       zlib libjpeg libwebp libpng libtiff libjasper IlmImf libprotobuf
--
--   OpenCV modules:
--     To be built:                 core flann imgproc ml photo reg surface_matching video dnn fuzzy imgcodecs shape videoio highgui objdetect plot superres ts xobjdetect xphoto bgsegm bioinspired dpm face features2d line_descriptor saliency text calib3d ccalib datasets rgbd stereo structured_light tracking videostab xfeatures2d ximgproc aruco optflow stitching
--     Disabled:                    world contrib_world
--     Disabled by dependency:      -
--     Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python2 python3 viz cvv hdf matlab sfm
--
--   Windows RT support:            NO
--
--   GUI:
--     QT:                          NO
--     Win32 UI:                    YES
--     OpenGL support:              NO
--     VTK support:                 NO
--
--   Media I/O:
--     ZLib:                        build (ver 1.2.8)
--     JPEG:                        build (ver 90)
--     WEBP:                        build (ver 0.3.1)
--     PNG:                         build (ver 1.6.19)
--     TIFF:                        build (ver 42 - 4.0.2)
--     JPEG 2000:                   build (ver 1.900.1)
--     OpenEXR:                     build (ver 1.7.1)
--     GDAL:                        NO
--
--   Video I/O:
--     Video for Windows:           YES
--     DC1394 1.x:                  NO
--     DC1394 2.x:                  NO
--     FFMPEG:                      YES (prebuilt binaries)
--       codec:                     YES (ver 56.41.100)
--       format:                    YES (ver 56.36.101)
--       util:                      YES (ver 54.27.100)
--       swscale:                   YES (ver 3.1.101)
--       resample:                  NO
--       gentoo-style:              YES
--     GStreamer:                   NO
--     OpenNI:                      NO
--     OpenNI PrimeSensor Modules:  NO
--     OpenNI2:                     NO
--     PvAPI:                       NO
--     GigEVisionSDK:               NO
--     DirectShow:                  YES
--     Media Foundation:            NO
--     XIMEA:                       NO
--     Intel PerC:                  NO
--
--   Parallel framework:            Concurrency
--
--   Other third-party libraries:
--     Use IPP:                     9.0.1 [9.0.1]
--          at:                     C:/Opencv310/sources/3rdparty/ippicv/unpack/ippicv_win
--     Use IPP Async:               NO
--     Use Eigen:                   NO
--     Use Cuda:                    NO
--     Use OpenCL:                  YES
--     Use custom HAL:              NO
--
--   OpenCL:
--     Version:                     dynamic
--     Include path:                C:/Opencv310/sources/3rdparty/include/opencl/1.2
--     Use AMDFFT:                  NO
--     Use AMDBLAS:                 NO
--
--   Python 2:
--     Interpreter:                 C:/Python27/python.exe (ver 2.7.12)
--
--   Python 3:
--     Interpreter:                 NO
--
--   Python (for build):            C:/Python27/python.exe
--
--   Java:
--     ant:                         NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
--
--   Matlab:                        Matlab not found or implicitly disabled
--
--   Documentation:
--     Doxygen:                     NO
--     PlantUML:                    NO
--
--   Tests and samples:
--     Tests:                       YES
--     Performance tests:           YES
--     C/C++ Examples:              NO
--
--   Install path:                  C:/Opencv310/build/install
--
--   cvconfig.h is in:              C:/Opencv310/build
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Opencv310/build

Comment: what bit version are you trying to compile? Is it 64-bit or 32-bit?
Answer:
--     CMake generator:             Visual Studio 14 2015

You've generated a Visual Studio 2015 solution. Run cmake with -G "NMake Makefiles" or "MinGW Makefiles" if you don't want to use VS.

No makefile created for opencv_contrib