Add column in dataframe based on 3 columns from another dataframe using R

Question: I have 2 dataframes which are as follows:
Dataframe 1: traffic_df which is hourly data.

Date_Time
Traffic

2020-03-09 06:00:00
10

2020-03-09 07:00:00
20

2020-03-10 07:00:00
20

2020-03-24 08:00:00
15

Dataframe 2: Alert.level

Start
End
Alert.level

10/03/2020 13:30
23/03/2020 13:30
2

23/03/2020 13:30
25/03/2020 23:59
3

I want to add a 3rd column to traffic_df which is the associated Alert.level if the Date_Time falls within the Start and End Date_Time of the Alert.level df so that the resulting dataframe will look like this:
Dataframe 1: traffic_df

Date_Time
Traffic
Alert.Level

2020-03-09 06:00:00
10

2020-03-09 07:00:00
20

2020-03-10 07:00:00
20
2

2020-03-24 08:00:00
15
3

Is there anyway to do this without having to make a matching hourly dataframe and then using join?
I'm thinking somehow using the map function?
Code to produce the df:
traffic_df <- structure(list(Date_Time = c("2020-03-09 06:00:00", "2020-03-09 07:00:00", "2020-03-10 07:00:00",
                              "2020-03-24 08:00:00"), Traffic = c(10L, 20L, 20L, 15L)),
                row.names = c(NA, -4L), class = "data.frame")

Alert.Level = data.frame(Start = c("10/03/2020 13:30", "23/03/2020 13:30"),
           End = c("23/03/2020 13:30", "25/03/2020 23:59"),
           Alert.level = c(2, 3))

Answer: You may try the fuzzyjoin package.
Data
library(lubridate)

traffic_df <- structure(list(Date_Time = c("2020-03-09 06:00:00", "2020-03-09 07:00:00", "2020-03-10 07:00:00",
                                           "2020-03-24 08:00:00"), Traffic = c(10L, 20L, 20L, 15L)),
                        row.names = c(NA, -4L), class = "data.frame") %>%
  mutate(Date_Time = ymd_hms(Date_Time))

Alert.Level = data.frame(Start = c("10/03/2020 13:30", "23/03/2020 13:30"),
                         End = c("23/03/2020 13:30", "25/03/2020 23:59"),
                         Alert.level = c(2, 3)) %>%
  mutate(Start = dmy_hms(Start),
         End = dmy_hms(End))

Code
library(fuzzyjoin)

traffic_df %>%
  fuzzy_left_join(Alert.Level,
                  match_fun = list(`>=`, `<=`),
                  by = list(x = c("Date_Time",
                                  "Date_Time"),
                            y = c("Start",
                                  "End"))) %>%
  select(-Start, -End)

Output
In contrast to your expected output above, row three is not matched, because 7:00 o'clock is before the starting time of 13:30.
            Date_Time Traffic Alert.level
1 2020-03-09 06:00:00      10          NA
2 2020-03-09 07:00:00      20          NA
3 2020-03-10 07:00:00      20          NA
4 2020-03-24 08:00:00      15           3

Answer: Here is a solution using sqldf. Note that I renamed the data.frame to have an underscore for convenience with SQL.
library(sqldf)

Alert_level <- Alert.level

sqldf("SELECT * FROM traffic_df
       LEFT JOIN Alert_level
       ON traffic_df.Date_Time BETWEEN Alert_level.Start AND Alert_level.End")

Output
            Date_Time Traffic               Start                 End Alert.level
1 2020-03-09 06:00:00      10                <NA>                <NA>          NA
2 2020-03-09 07:00:00      20                <NA>                <NA>          NA
3 2020-03-10 07:00:00      20                <NA>                <NA>          NA
4 2020-03-24 08:00:00      15 2020-03-23 13:30:00 2020-03-25 23:59:00           3

Answer: I like outer approaches in such cases. First, define a Vectorized FUNction, that looks if a specific x is between an y interval. Put it in outer which iterates each Date_Time with each start/end interval of Alert.Level. This gives a matrix o that informs which of the intervals is applicable (I use unname to avoid confusion). Then, in traffic_df we crate a NA column alert_lv (should just have a different name than "Alert.Level"), subset it with positive colSums, and put in the according levels of Alert.Level.
FUN <- Vectorize(function(x, y) x >= y[1] & x < y[2])
(o <- unname(outer(traffic_df$Date_Time, Alert.Level[-3], FUN)))
#       [,1]  [,2]  [,3]  [,4]
# [1,] FALSE FALSE  TRUE FALSE
# [2,] FALSE FALSE FALSE  TRUE

w <- unlist(apply(o, 1, which))

traffic_df <- within(traffic_df, {
  alert_lv <- NA
  alert_lv[rowSums(o) > 0] <- Alert.Level[w, 3]
})
traffic_df
#             Date_Time Traffic alert_lv
# 1 2020-03-09 06:00:00      10       NA
# 2 2020-03-09 07:00:00      20       NA
# 3 2020-03-10 07:00:00      20        2
# 4 2020-03-24 08:00:00      15        3

Note: To use this solution you first need the usual 'POSIXct' formats, so first you should do
traffic_df$Date_Time <- as.POSIXct(traffic_df$Date_Time)
Alert.Level[1:2] <- lapply(Alert.Level[1:2], strptime, format='%d/%m/%Y %H:%M')


Subtree of Another Tree Complexity Analysis

Question: On Leetcode, Question #572 prompts the user to check whether a particular tree (given by its root) is a subtree of another tree (given by its root). Below is an implementation to solve this problem:
class Solution:
    def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
        def isSameTree(p, q):
            if not p and not q:
                return True
            if not p or not q or p.val != q.val:
                return False

            return isSameTree(p.left, q.left) and isSameTree(p.right, q.right)

        def recurse(root, subRoot):
            if not root:
                return False
            elif isSameTree(root, subRoot):
                return True

            return recurse(root.right, subRoot) or recurse(root.left, subRoot)

        return recurse(root, subRoot)

In effect, the recurse() call will be called once for each node in the parent tree. During a given call, iSameTree() will be called an attempt to "super-impose" the subtree onto the tree, starting at the current node. If it can successfully match all components of the subtree, then iSameTree() will return True. Otherwise, it will return False.
While the implementation is a form of brute force, I am having a harder time discussing time and space complexity. For the intent of analysis, let us say that the size of the potential parent tree in nodes is given by N whereas the size of the potential subtree in nodes is given by M.
When it comes to time complexity, we could quickly conclude that there will be N calls to recurse(), with each having the potential to generate a maximum of M calls to isSameTree(). Therefore, at worst, there will be N*M total calls. Apart from the recursive component, each function call takes constant time to perform, so we can say that this algorithm is worst-case O(NM). While this makes sense, I feel as though there may be a tighter bound. In other words, for an arbitrary N node parent tree and M node subtree, it will be impossible for every one of the N recurse() calls to lead to M IsSubtree() calls; for instance, leaf nodes will naturally terminate after the first IsSameTree() call; so, if M is not size 1, then the recurse() calls associated with leaf nodes will result in fewer than M calls in every other scenario. I say all of this to ask: Is there a way to find a tighter bound?
When it comes to auxiliary space complexity, it is perhaps even more complicated. Some folks are quick to assert that the space complexity is O(LogN + LogM), but, again, this does not make sense to me. Moreover, at any given time, we can have a maximum of LogN recurse() calls on the call stack. At any given time, we can also have a maximum of LogM isSameTree() calls on the call stack. While these two statements are true, they are never simultaneously true for an arbitrary N and M. That is, to achieve LogM isSameTree() calls on the call stack, the most recent recurse() call needs to be on a node that is at least LogM levels from the leaf nodes in the parent tree; therefore, the deepest level that the node can be is LogN - LogM. It follows that there can only be LogN - LogM recurse() calls and LogM isSameTree calls on the call stack at a given time; in total, that is LogN calls. On the flip side, for there to be LogN recurse() calls on the call stack, the most recent call to recurse() needs to be on a leaf node in the parent tree. In such a case, this recurse() call can only lead to, at most, 2 IsSubtree() additional calls on the call stack. Based on this logic, then, the auxiliary space is simply O(LogN). I say all of this to ask: is auxiliary space O(LogN) or O(LogN + LogM)?
Answer: The analysis is easier than you think, because the worst case inputs (for both time and space) are completely unbalanced trees, i.e., linked lists, with all values the same.
Worst case time complexity is Θ(NM).  When M=N/2, for example, you will do N/2 comparisons of size M, and NM/2 is in Ω(NM).
Worst case space complexity is Θ(N).  You'll never recurse deeper than depth N, and you will get to depth N no matter what M is.
You can make a simple linear time algorithm by only comparing the subtree-to-find with subtrees of the same height.


is f an rienmann intergrable

Question:
For a function to be Rienmann integral, its upper integral and lower integral need to be the same. I don't understand the function they have given here where $x=r/2^n$ thus don't know how to apply to values of the bounds to the functions. Also how to work out the upper and lower sum from this?
Comment: Twice you wrote it incorrectly: it is **Riemann** .
Comment: @K_uddin I've edited to focus on $f$ as you've mentioned in your title, and retyped the question and kept the original image linked. Just in case, you were asking about $f$ and not $f_n$ I presume? Although I imagine both questions will have a some similarities in their analysis.
Answer: Hint: for each fixed $n$ there is only a finite number of points of $x\in[0,1]$ s.t. $x = r/2^n$, $r\in\Bbb Z$. But the set of points $r/2^n\in[0,1]$ for some $r,n\in\Bbb Z$ is dense in $[0,1]$ (and in any subinterval).
Answer: For any fixed $n$ the function is zero on the whole interval up to $0,1/2^n,2/2^n,\dots, (2^n-1)/2^n,1$, where its value is $1$.
For example is $n=3$, then your function $f_3$ is $1$ at $0,1/8,2/8,3/8,4/8,5/8,6/8,7/8,8/8$ and zero everywhere else.
$f$ looks basically like $f_n$ but has more peaks: it is $1$ where $x=r/2^n$, $r,n\in\mathbb{N}$. So it is $1$ at $0,3/4,7/8,10/1224\ $ but not at $5/7,9/10,3/6\ $ or any irrational number.
Comment: Note that we are not talking about multiple functions parametrized by $n$ here.
Comment: @Vik78 Well there was an $f_n$ in the original attached image as you can still check, but since the o.p. mentioned $f$ and not $f_n$ I presumed they meant $f$. I retyped the question to make it clearer which one the question was presumed to be about.
Comment: Exactly @snulty. I've answered how to interpret the first function $f_n$ of the first question.
Comment: I can roll the edit back until the op responds, since both of the present answers address part (a).

is f an rienmann intergrable


C# SmtpClient.Send() is slow because of BCC

Question: I'm using "System.Net.Mail" for send email on our Intranet system in C#
The Send() method was very slow and I did not understand why.
After debugging, I've remove the BCC call : (MM is a MailMessage() and Item, a string email address) :
<code>MM.Bcc.Add(new MailAddress(Item));
</code>
Before I comment this line, an email was taking about 30 or 40 seconds. Now, it's about 2 seconds. Why?
Is there an explication about this? I can add "To" and "CC" with no problem of performance. But not "BCC".
Comment: Are you sure it's .NET and not your mail server?
Comment: How many BCC addresses are you sending to? Each one is a separate send...
Comment: Does the delay happen on the send or on Bcc.Add?
Comment: How many BCC are you adding?
Comment: CodeCaster has the intuition - it may be your mail server not handling BCC well, esp. if the list is huge. Peek at this - http://www-01.ibm.com/support/docview.wss?uid=swg21089346
Comment: I'm adding 4 emails in BCC and during the debug, the delay is on the "Send()" method line. But, with the same SMTP server, on outlook, the same email and recipients does not take time.
Comment: Might be helpful to Fiddle the .NET app vs Outlook...
Comment: Also try different SMTP server (maybe gmail?)
Comment: I can't use an external SMTP server. I'm not allowed.
Comment: Ok, I've try with the smtp server "auth.smtp.1and1.fr" and it's very fast with Bcc recipients. But, why it's slow in an App .Net and fast in Outlook? I don't know...
Comment: Did you try to add all email addresses at once? `xxx.Bcc.Add("firstname.lastname@example.com,email@example.com,firstname.lastname@example.com");`
Comment: It's the same : too long
Answer: I had a similar problem and this drove me crazy trying to figure out what made some emails slow and others quick.
The first email sent after creating SmtpClient takes 5 seconds.  After that, reusing the same SmtpClient, it's about 0.4 seconds if done within 60 seconds of the previous one.  After that I think it releases it's connection to the pool.
However adding a CC that is a duplicate of the To address will always add an additional 5 seconds. Adding a duplicate CC twice will add 10 seconds.
I didn't check, but I assume the same would be true if duplicates are in the "TO" or "BCC" lists.
Here's the code to remove duplicates:
<code>// check and remove duplicates
for (int x = 0; x < message.To.Count; x++)
{
    for (int y = message.To.Count - 1; y > x; y--)
    {
        if (message.To[y].Address == message.To[x].Address)
            message.To.RemoveAt(y);
    }
    for (int y = message.CC.Count - 1; y >= 0; y--)
    {
        if (message.CC[y].Address == message.To[x].Address)
            message.CC.RemoveAt(y);
    }
    for (int y = message.Bcc.Count - 1; y >= 0; y--)
    {
        if (message.Bcc[y].Address == message.To[x].Address)
            message.Bcc.RemoveAt(y);
    }
}
for (int x = 0; x < message.CC.Count; x++)
{
    for (int y = message.CC.Count - 1; y > x; y--)
    {
        if (message.CC[y].Address == message.CC[x].Address)
            message.CC.RemoveAt(y);
    }
    for (int y = message.Bcc.Count - 1; y >= 0; y--)
    {
        if (message.Bcc[y].Address == message.CC[x].Address)
            message.Bcc.RemoveAt(y);
    }
}
for (int x = 0; x < message.Bcc.Count; x++)
{
    for (int y = message.Bcc.Count - 1; y > x; y--)
    {
        if (message.Bcc[y].Address == message.Bcc[x].Address)
            message.Bcc.RemoveAt(y);
    }
}
</code>
Comment: Many thanks. We ran into this. Very strange behaviour. I assume it is a bug.


How to set all locale settings in Ubuntu

Question: A remote installed application has some encoding problems and on my local machine it is running fine.
What is the best way to "copy" my locales to the remote machine?
The locales on my personal machine are configured like this:
$ locale
LANG=de_DE.UTF-8
LANGUAGE=de_DE:en
LC_CTYPE="de_DE.UTF-8"
LC_NUMERIC=en_US.UTF-8
LC_TIME=en_US.UTF-8
LC_COLLATE="de_DE.UTF-8"
LC_MONETARY=en_US.UTF-8
LC_MESSAGES="de_DE.UTF-8"
LC_PAPER=en_US.UTF-8
LC_NAME=en_US.UTF-8
LC_ADDRESS=en_US.UTF-8
LC_TELEPHONE=en_US.UTF-8
LC_MEASUREMENT=en_US.UTF-8
LC_IDENTIFICATION=en_US.UTF-8
LC_ALL=

Comment: a user-specific locale (for one user) or generally for the whole system?
Answer: You can set locale manually using update-locale:
sudo update-locale LANG=de_DE.UTF-8 LC_MESSAGES=POSIX

Read the man page for more information.
Alternatively, you can manually change your system's locale entries by modifying the file /etc/default/locale.
For example, on a German system, to prevent system messages from being translated, you may use:
LANG=de_DE.UTF-8
LC_MESSAGES=POSIX

Note: changes take effect only after a fresh login.
Source: https://help.ubuntu.com/community/Locale
Comment: Run the following command: `sudo dpkg-reconfigure locales`.
Comment: I think update-locale needs `sudo`. Personally I like your alternative solution, maybe you should make it a primary one. :)
Comment: updated it!! :)
Comment: "perl: warning: Setting locale failed." .. How to fix this ?
Comment: @green I have the same issue and `dpkg-reconfigure locales` seems not setting local in /etc/default/locale. is there a way to apply update-locale for all ?
Answer: The easier way

Export all locales into a file
locale > import

Open file and add export at the start of each line

Make it executable with the command chmod ugo+rx import

Copy to desired profile and execute ./import
Comment: (a) "Copy to desired profile" -- does this mean to move the file to the home of the user (b) Is this a permanent solution?
Comment: And what would have been the shell script to write the import on each line ?
Comment: Didn't work for me. I tried to do it from my computer to another ssh.
Answer: There are some recommendations when configuring locales in remote machines

In Debian machines (remote machine), run the command (as root):
sudo dpkg-reconfigure locales

On the first screen, select the desired locales. After that, you will be prompted to choose which is the default locale. Select "none" (reference: https://wiki.debian.org/Locale#Standard).

Configure your ssh service (/etc/ssh/sshd_config) to accept environment variables from the client:
uncomment the line:
AcceptEnv LANG LC_*

Restart your ssh server, logoff and log back in and run the locale command. It must match your local machine's locale.
Comment: You said "On the first screen, select the desired locales", but I have tested on Ubuntu V14.04.2LTS and all the process is done automatically (`Generating locales...` ... and later `Generation complete.`) I get no selection screen.
Comment: @SopalajodeArrierez did you solve that?
Comment: Well, as for today, I am using v16.04 and, indeed, the "first screen" exists and I can select the desired locales. Maybe it was just
a problem with v14.
Comment: Nice! Sorry about my **huge** delay. I didn't know how to use this tool that time and forgot about my account since then. Best regards.


Java time-based map/cache with expiring keys

Question: Do any of you know of a Java Map or similar standard data store that automatically purges entries after a given timeout? This means aging, where the old expired entries "age-out" automatically.
I know of ways to implement the functionality myself and have done it several times in the past, so I'm not asking for advice in that respect, but for pointers to a good reference implementation.
WeakReference based solutions like WeakHashMap are not an option, because my keys are likely to be non-interned strings and I want a configurable timeout that's not dependent on the garbage collector.
Ehcache is also an option I wouldn't like to rely on because it needs external configuration files. I am looking for a code-only solution.
Comment: This question is beeing discuted on [meta](https://meta.stackoverflow.com/q/421689/10952503) about the close reason.
Answer: Yes. Google Collections, or Guava as it is named now has something called MapMaker which can do exactly that.
ConcurrentMap<Key, Graph> graphs = new MapMaker()
   .concurrencyLevel(4)
   .softKeys()
   .weakValues()
   .maximumSize(10000)
   .expiration(10, TimeUnit.MINUTES)
   .makeComputingMap(
       new Function<Key, Graph>() {
         public Graph apply(Key key) {
           return createExpensiveGraph(key);
         }
       });

Update:
As of guava 10.0 (released September 28, 2011) many of these MapMaker methods have been deprecated in favour of the new CacheBuilder:
LoadingCache<Key, Graph> graphs = CacheBuilder.newBuilder()
    .maximumSize(10000)
    .expireAfterWrite(10, TimeUnit.MINUTES)
    .build(
        new CacheLoader<Key, Graph>() {
          public Graph load(Key key) throws AnyException {
            return createExpensiveGraph(key);
          }
        });

Comment: *Warning*! Using `weakKeys()` imply that keys are compared using the == semantics, not `equals()`. I lost 30 minutes figuring out why my String-keyed cache was not working :)
Comment: As from v10, you should be using CacheBuilder instead (http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/cache/CacheBuilder.html) since expiration etc have been deprecated in MapMaker
Comment: Folks, thing that @Laurent mentioned about `weakKeys()` is important. `weakKeys()` is not required 90% of the time.
Comment: How would you implement createExpensiveGraph() with a simple map-based approach that also should support .put() ?
Comment: @ShervinAsgari for the sake of beginners (myself included), could you switch your updated guava example to one that uses Cache instead of LoadingCache? It would match the question better (since LoadingCache has features that exceed a map with expiring entries and is far more complicated to create) see https://github.com/google/guava/wiki/CachesExplained#from-a-callable
Comment: how to implement createExpensiveGraph(key)?
Comment: Sorry, I still not understand,i guess the load(key key) is the map.get(key) method. Now, I have a map, and i don't want the key-value existing after 15 minutes of last access,  would you please implement it in a small example? Also, can I use Cache just like a map?
Comment: I know you have just copied the example from the javadoc, but as I ran into the same requirement, I just want to make sure the map properties are correct: Wouldn't it be better to use `expireAfterAccess` (instead of `expiration`) and `softValues` (instead of `weakValues`)?
Comment: @Jaskey Its just an example of a method that adds the content in the cache based on the key
Comment: @Jaskey the return value from the load method is what is put in the Cache. So in this example the createExpensiveGraph(key) method could just as well return new Graph() and it would put the new Graph() in the map with the key as given
Comment: Is there a `graphs.remove("key")` like a map? How do I remove an entry before expiry time?
Comment: Never mind. Got it `graphs.invalidate("key")`.
Comment: @Jeutnarg I had the same question as you. As of Guava 27.0.1, if you call `CacheBuilder.build()` with no CacheLoader argument, then the returned type is a Cache, not a CacheLoader.

I'm struggling to format this, and it's Kotlin, not Java, but I hope it helps someone.

```val resultCache: Cache = CacheBuilder.newBuilder().expireAfterWrite(500, TimeUnit.MILLISECONDS).build()

    // elsewhere, when you have the entry you want
    resultCache.put(myString, myValueObject)```
Comment: @ShervinAsgari great answer. Do you know if it is possible to set expiration time per key on demand? For instance, I have a `users` cache, so I want regular users to expire in 30min but admin to expire in 10min. Is this doable with Guava or should I have 2 separated caches?
Comment: Is this support concurrency ?
Answer: This is a sample implementation that i did for the same requirement and concurrency works well. Might be useful for someone.
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * @author Vivekananthan M
 *
 * @param <K>
 * @param <V>
 */
public class WeakConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {

    private static final long serialVersionUID = 1L;

    private Map<K, Long> timeMap = new ConcurrentHashMap<K, Long>();
    private long expiryInMillis = 1000;
    private static final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss:SSS");

    public WeakConcurrentHashMap() {
        initialize();
    }

    public WeakConcurrentHashMap(long expiryInMillis) {
        this.expiryInMillis = expiryInMillis;
        initialize();
    }

    void initialize() {
        new CleanerThread().start();
    }

    @Override
    public V put(K key, V value) {
        Date date = new Date();
        timeMap.put(key, date.getTime());
        System.out.println("Inserting : " + sdf.format(date) + " : " + key + " : " + value);
        V returnVal = super.put(key, value);
        return returnVal;
    }

    @Override
    public void putAll(Map<? extends K, ? extends V> m) {
        for (K key : m.keySet()) {
            put(key, m.get(key));
        }
    }

    @Override
    public V putIfAbsent(K key, V value) {
        if (!containsKey(key))
            return put(key, value);
        else
            return get(key);
    }

    class CleanerThread extends Thread {
        @Override
        public void run() {
            System.out.println("Initiating Cleaner Thread..");
            while (true) {
                cleanMap();
                try {
                    Thread.sleep(expiryInMillis / 2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        private void cleanMap() {
            long currentTime = new Date().getTime();
            for (K key : timeMap.keySet()) {
                if (currentTime > (timeMap.get(key) + expiryInMillis)) {
                    V value = remove(key);
                    timeMap.remove(key);
                    System.out.println("Removing : " + sdf.format(new Date()) + " : " + key + " : " + value);
                }
            }
        }
    }
}

Git Repo Link (With Listener Implementation)
https://github.com/vivekjustthink/WeakConcurrentHashMap
Cheers!!
Comment: Can it be like, in get u check if the value is old than return null. Instead of cleanup thread.
  as in ... https://stackoverflow.com/a/63189871/3509609
Comment: Why do you do execute `cleanMap()` half the time expecified?
Comment: Bcoz it makes sure the keys are expired (removed) and avoids thread from extreme looping.
Comment: @Vivek but with this implementation there can be at max  (expiryInMillis / 2) number of entries which are already expired but still present in cache. As thread deletes entries after expiryInMillis / 2 period
Comment: @rishi007bansod True, but that's compromisable for faster gets. If your requirement demands 100% accurate expiry then you need to check and clean value on every get
Comment: @Vikash as mentioned in the previous comment cleanup thread is to improve performance of get, and values don't last in memory for longer than needed. If implemented in get and if the values aren't accessed then it lasts in memory forever.
Comment: @Vivek with a weakHashMap, the data gets removed
1. If the cleanMap detects an item to have lived beyond it's eviction time.
2. If GC detects items that can be removed.
So, depending on which happens first, we do not guarantee the presence of the data.
This is not what the OP asked for.
Comment: SimpleDateFormat is not thread safe.
Answer: Apache Commons has decorator for Map to expire entries: PassiveExpiringMap
It's more simple than caches from Guava.
P.S. be careful, it's not synchronized.
Comment: It is simple, but it checks the expiration time only after you access an entry.
Comment: If using PassiveExpiringMap along with Collections.synchronizedMap, notice that access to the map collections (values, keySet, entrySet) will not trigger an eviction of the expired entries. Reason being that values, keySet and entrySet are "cached" in the SynchronizedMap.
Comment: As per the [Javadoc](https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/map/PassiveExpiringMap.html): _When invoking methods that involve accessing the entire map contents (i.e containsKey(Object), entrySet(), etc.) this decorator removes all expired entries prior to actually completing the invocation._
Comment: If you want to see what the latest version of this library (Apache commons commons-collections4) is [here is a link to the relevant library on mvnrepository](https://mvnrepository.com/artifact/org.apache.commons/commons-collections4)
Comment: implementation hint: it has a **Lazy/Passive Eviction Process**, so it removeAllExpired before performing methods, so if you don't call any methods the expired items will not be removed until you call any method on the map
Answer: You can try out my implementation of a self-expiring hash map. This implementation does not make use of  threads to remove expired entries, instead it uses DelayQueue that is cleaned up at every operation automatically.
Comment: Another problem: you can't put the same key twice with different lifetimes. After a) put(1, 1, shortLived), then b) put(1, 2, longLived) the Map entry for key 1 will be gone after shortLived ms no matter how long longLived is.
Comment: I like Guava's version better, but +1 for adding completeness to the picture
Comment: @piero86 I'd say the call to delayQueue.poll() in method expireKey(ExpiringKey delayedKey) is wrong. You may loose an arbitrary ExpiringKey which can't later be utilized in cleanup() - a leak.
Comment: Thank you for your insight. Could you report these issues as comments in the gist, please?
Comment: Fixed according to your suggestions. Thanks.
Comment: @SeanPatrickFloyd it's completely different. This implementation has TTL for each item, Guava uzes same TTL for all items.

On;y problem with this code is that it's not thread safe, but piero86 really thanks a lot, you saved my day.
Comment: Note that Guava is also performing clean up during write and read operations. This is not to say Guava is better, only that it works in the same way (in fact I would lean toward simplicity while Guava is massive).
Comment: @pcan: Do we need WeakHashMap for expiringKeys? An Entry in a WeakHashMap is automatically GC when its key is no longer in use. In your implementation, keys are removed manually using remove() method which defeats the purpose of WeakHashMap.
Answer: Sounds like ehcache is overkill for what you want, however note that it does not need external configuration files.
It is generally a good idea to move configuration into a declarative configuration files ( so you don't need to recompile when a new installation requires a different expiry time ), but it is not at all required, you can still configure it programmatically.  http://www.ehcache.org/documentation/user-guide/configuration
Answer: If anybody needs a simple thing, following is a simple key-expiring set. It might be converted to a map easily.
public class CacheSet<K> {
    public static final int TIME_OUT = 86400 * 1000;

    LinkedHashMap<K, Hit> linkedHashMap = new LinkedHashMap<K, Hit>() {
        @Override
        protected boolean removeEldestEntry(Map.Entry<K, Hit> eldest) {
            final long time = System.currentTimeMillis();
            if( time - eldest.getValue().time > TIME_OUT) {
                Iterator<Hit> i = values().iterator();

                i.next();
                do {
                    i.remove();
                } while( i.hasNext() && time - i.next().time > TIME_OUT );
            }
            return false;
        }
    };

    public boolean putIfNotExists(K key) {
        Hit value = linkedHashMap.get(key);
        if( value != null ) {
            return false;
        }

        linkedHashMap.put(key, new Hit());
        return true;
    }

    private static class Hit {
        final long time;

        Hit() {
            this.time = System.currentTimeMillis();
        }
    }
}

Comment: This is fine for a single-thread situation, but it would break miserably in a concurrent situation.
Comment: @SeanPatrickFloyd you mean like LinkedHashMap's itself?! "it must be synchronized externally" just like LinkedHashMap, HashMap ... you name it.
Comment: yes, like all those, but unlike Guava's cache (the accepted answer)
Comment: Also, consider using `System.nanoTime()` for computing time differences as System.currentTimeMillis() is not consistent as it depends on the system time and might not be continous.
Answer: Typically, a cache should keep objects around some time and shall expose of them some time later. What is a good time to hold an object depends on the use case. I wanted this thing to be simple, no threads or schedulers. This approach works for me. Unlike SoftReferences, objects are guaranteed to be available some minimum amount of time. However, the do not stay around in memory until the sun turns into a red giant.
As useage example think of a slowly responding system that shall be able to check if a request has been done quite recently, and in that case not to perform the requested action twice, even if a hectic user hits the button several times. But, if the same action is requested some time later, it shall be performed again.
class Cache<T> {
    long avg, count, created, max, min;
    Map<T, Long> map = new HashMap<T, Long>();

    /**
     * @param min   minimal time [ns] to hold an object
     * @param max   maximal time [ns] to hold an object
     */
    Cache(long min, long max) {
        created = System.nanoTime();
        this.min = min;
        this.max = max;
        avg = (min + max) / 2;
    }

    boolean add(T e) {
        boolean result = map.put(e, Long.valueOf(System.nanoTime())) != null;
        onAccess();
        return result;
    }

    boolean contains(Object o) {
        boolean result = map.containsKey(o);
        onAccess();
        return result;
    }

    private void onAccess() {
        count++;
        long now = System.nanoTime();
        for (Iterator<Entry<T, Long>> it = map.entrySet().iterator(); it.hasNext();) {
            long t = it.next().getValue();
            if (now > t + min && (now > t + max || now + (now - created) / count > t + avg)) {
                it.remove();
            }
        }
    }
}

Comment: HashMap is not thread safe, due to race conditions, map.put operation or resizing of map may lead to data corruption. See here: https://mailinator.blogspot.com/2009/06/beautiful-race-condition.html
Comment: That's true. Indeed, most Java classes are not thread safe. If you need thread security, you need to check every affected class of your design to see if it meets the requirement.
Answer: you can try Expiring Map
http://www.java2s.com/Code/Java/Collections-Data-Structure/ExpiringMap.htm
a class from The Apache MINA Project


Only run Ansible task on box in specific group

Question: I am trying to create a task that will only run on a box that is in a specific group (called pi).
I am using Ansible version:
ansible 184.108.40.206
  config file = /Users/user/Development/raspbian-homeassistant/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 3.6.3 (default, Dec  3 2017, 10:37:53) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.38)]

This is my current code:
- name: Set up zwave USB dongle
  when: inventory_hostname in groups['pi']
  blockinfile:
    path: /var/local/homeassistant/.homeassistant/configuration.yaml
    marker: "# {mark} ANSIBLE MANAGED BLOCK #"
    insertafter: "# zwave dongle"
    content: |2
      zwave:
        usb_path: /dev/ttyACM0
  tags:
    - config
    - hass

It seems to work correctly when the hostname is in the group but throws an error when not.
This is the error I get when I run it on my vagrant box (in group vagrant):
fatal: [192.168.33.123]: FAILED! => {"failed": true, "msg": "The conditional check 'inventory_hostname in groups['pi']' failed. The error was: error while evaluating conditional (inventory_hostname in groups['pi']): Unable to look up a name or access an attribute in template string ({% if inventory_hostname in groups['pi'] %} True {% else %} False {% endif %}).\nMake sure your variable name does not contain invalid characters like '-': argument of type 'StrictUndefined' is not iterable\n\nThe error appears to have been in '/Users/andy/Development/raspbian-homeassistant/ansible/roles/configure-hass/tasks/main.yml': line 21, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Set up zwave USB dongle\n  ^ here\n"}

Check if a list contains an item in Ansible suggests I have the syntax right but I guess that is for an older version of Ansible?
How do I fix this?
Answer: Generally, if you want tasks to apply only to hosts in a particular group, the way you do that is by creating a play that targets that group:
- hosts: pi
  tasks:
    - name: Set up zwave USB dongle
      blockinfile:
        path: /var/local/homeassistant/.homeassistant/configuration.yaml
        marker: "# {mark} ANSIBLE MANAGED BLOCK #"
        insertafter: "# zwave dongle"
        content: |2
          zwave:
            usb_path: /dev/ttyACM0
      tags:
        - config
        - hass

The error you're getting is because groups['pi']is undefined.  There are a couple of ways of preventing the error.  For example, you can explicitly check thatgroups['pi']` is defined before trying to use it:
- name: set up zwave USB dongle
  when: groups['pi'] is defined and inventory_hostname in groups['pi']

Or you can use the default filter to provide a default value:
- name: set up zwave USB dongle
  when: inventory_hostname in groups['pi']|default([])


Access specific port run by services using elastic ip

Question: I have windows server 2008 R2  running in AWS (with Elastic IP)where i am running Apache service to host one of website which i can easily access using various method like

Like "localhost:port" works fine
Using server NIC ip address:port works fine.

In my current use case where i want to expose this website on my elastic IP i am not able to do so.
However if i host any website on my IIS i can access or view using my elastic IP but i am unable to host apache website on IIS.
Whenever i try to access apache one it never worked.

I tweaked firewall setting
I also updated conf file of apache whenever i try to give elastic IP address it will not start.

Failed with following error message
 The Apache service named  reported the following error:
>>> (OS 10049)The requested address is not valid in its context.  : make_sock: could not bind to address 52.xx.xx.xx:8888

Following is my service details of apache on services.msc
C:\Program Files (x86)\vcollapp\apache\bin\Apache.exe -k runservice

Now how can i expose my site which is running on apache service inside windows to elastic ip of AWS.
Thanks in Advance for your help and time.
Answer: Try binding to 0.0.0.0? This will accept connections from all interfaces, not just localhost.
If you check your network interfaces, do you see an interface with the Elastic IP? Or do you just have your Private IP?
There are different ways of using Elastic IP's, and my assumption here is that the IP is not physically attached to your machine, but instead, you have a private IP that all traffic from the Elastic IP is routed through.
WAN -> Elastic -> Private

Comment: Thanks Matt for replying as i can see my network interfaces i can see that it is only private IP.
Comment: Is there any way that i can publish ...it... like other provider i do get n/w interface that will hold public IP but it seems to be #AWS it is not feasible ...


Python, returning data collected in npyscreen

Question: So I'm creating a basic TUI for a script I created. The goal is to collect several variables that include paths to directories and files. What I'm not sure about is once I've created the visual aspect of it, how to get those pieces of information to interact with other parts of the code.
Below is what I have so far in terms of the visual portion (the other part is about 500 lines), and honestly I'm at a loss on how to even print any of the variables set under the class and any pointers would be greatly appreciated.
<code>#!/usr/bin/env python
# encoding: utf-8

import npyscreen

class PlistCreator(npyscreen.NPSApp):
    def main(self):
        screen = npyscreen.Form(name="Welcome to Nicks Playlist Creator!")
        plistName = screen.add(npyscreen.TitleText, name="Playlist Name:" )
        csvPath = screen.add(npyscreen.TitleFilenameCombo, name="CSV Schedule:")
        toaPath = screen.add(npyscreen.TitleFilenameCombo, name="Path to ToA Video Folder:", use_two_lines=True)
        outputPath = screen.add(npyscreen.TitleFilenameCombo, name = "Output Folder:", use_two_lines=True)
        dateOfAir = screen.add(npyscreen.TitleDateCombo, name="Date of Air:")
        timeOfStart = screen.add(npyscreen.TitleText, name="Time of Air (TC):")
        screen.edit()

if __name__ == "__main__":
    App = PlistCreator()
    App.run()
</code>
Comment: Just from skimming the docs [here](http://npyscreen.readthedocs.io/application-structure.html), dsomething like `App.plistName`, `App.csvpath`, etc. work to get the values after entering? (You may need to assign them all to `self`, e.g. `self.screen = ...`)
Answer: You can get the value of any form object using the dit notation.
<code>plistName = screen.add(npyscreen.TitleText, name="Playlist Name:" )
playlist_name = self.screen.plistName.value
</code>
etc. Note there are no parentheses after value. Once you have it in a variable, you can build another method in the class to handle the information.


GitHub workflow after pull request review and commits

Question: I have forked a public repo on GitHub, created a branch, pushed the branch/changes to the forked repo and then created a pull request to the original repo.
Now the owner of the repo reviewed my code and requested some changes. Now what I want to do:

Update the code taking the review comments into account
Also I am seeing, that in the meantime, my branch is "1 commit ahead, 4 commits behind exampleRepo:main", what I also now want to take into account

My question: What is the workflow now for me to take everything into account?
From my point of view I see two options, but I am not sure which one is the right one without breaking something.
Option 1:

Update the code based on review comments in same branch
Commit changes in same branch as before
Push changes to my forked repo
Create pull request with last commit

Or Option 2:

First run "git pull" locally to get all recent changes (because my forked repo is "4 commits behind" as stated above)
Update the code based on review comments in same branch
Commit changes in same branch as before
Push changes to my forked repo
Create pull request with last commit

Or do you see another option and workflow I should use for that?
Answer: The message from git tells that someone else pushed to main since the time you made the PR.
You could still keep your PR if the files you changed don't conflict with main, but it might be cleaner to do a git rebase of your branch on top of main.
The rebase will rewrite the history of your branch and you will have to git push --force SO make sure nobody else is working on that branch.
The PR from github will then adjust automatically


Return Base class shared pointer using derived class C++

Question: I have a an interface:
/*base.hpp */

class Base {
    protected:
    Base() = default;

    public:
    Base(Base const &) = delete;
    Base &operator=(Base const &) = delete;
    Base(Base &&) = delete;
    Base &operator=(Base &&) = delete;
    virtual ~Base() = default;

    virtual void Function1() = 0;
};

Also there is a function in this interface:
std::shared_ptr<Base> **getBase**();   //this will return the instance of Base class

Since function in base class is pure virtual, sample Derived class is below:
#inclide "base.hpp"
class Derived : public Base
{
    public:
    Derived();
    ~Derived();
    virtual void Function1() override;
};

In main.cpp -> there is a call to getBase()
std::shared_ptr<Base> ptr{ nullptr };
void gettheproxy() {
    ptr = getBase(); //call to get baseclass instance
}

Implementation of getBase method (in separate file getBase.cpp)
#include "base.hpp"
#include "derived.hpp"

Derived d;
std::shared_ptr<Base> getBase()
{
    std::shared_ptr<Base> *b= &d;
    return b;
}

Error:
cannot convert 'Base*' to Base' in initialization
What is the correct way to get base class instance from derived class implementation?
Note: I have to follow this design of classes due to code dependency.
Comment: `std::shared_ptr b(&derived);` will do but it will try to delete the global static variable at some point, which is UB.
Comment: You'll get yourself into trouble sharing the ownership of an object between the stack and the shared pointer object. When the last copy of the shared pointer gets destroyed `delete` is called which will almost certainly crash your program. Btw: you cannot turn a pointer to `Derived` into a pointer to `std::shared_ptr` and even if you could you wouldn't be able to convert `std::shared_ptr*` to `std::shared_ptr` implicitly.
Comment: @fabian Nitpick: the object in this example is not on the stack. It's a global static object.
Answer: This should do:
std::shared_ptr<Derived> d = std::make_shared<Derived>();

std::shared_ptr<Base> getBase() { return d; }


Change of two normal coordinates based on two nearby points?

Question: Let $M$ be a manifold and $L(M)$ be the tangent frame bundle on $M$. Let $\Gamma$ be a linear connection on $L(M)$ which induces a covariant derivative $\nabla$ on $TM$.
Let $p, q$ be two distinguished and sufficiently close points on $M$, connected by a smooth curve $\gamma = \{\gamma_t\}_{t\in[0,\epsilon]}$ so that $\gamma(0)=p$, $\gamma(\epsilon)=q$. Suppose that there is an open set $U\subset M$, such that $U$ contains $\gamma$ and is a normal neighborhood of both $p$ and $q$.
Fix a linear frame $u_p \in L_p(M): \mathbf R^d\to T_pM$. Let $u_q = \Gamma(\gamma)_0^\epsilon (u_p) \in L_q(M)$ be the parallel displacement of $u_p$ along $\gamma$, that is, $u_q$ can be joined to $u_p$ by a horizontal curve on $L(M)$ along $\gamma$.
Then we have two normal coordinate systems on $U$:
$$x=u_p^{-1}\circ \exp_p^{-1}: U \to \mathbf R^d,$$
$$y=u_q^{-1}\circ \exp_q^{-1}: U \to \mathbf R^d,$$
so that $(U,(x^i))$ and $(U,(y^j))$ are coordinate charts based on $p$ and $q$ respectively.

Now my question is: how to do change of coordinates between these two normal coordinate charts? More precisely, let $m \in U$, then what is the relation between $x(m)$ and $y(m)$?
If necessary, you can endow more structures to $M$. Say, $M$ is equipped with a Riemannian metric $g$ and $\nabla$ is the Levi-Civita connection, the frame bundle $L(M)$ is replaced by the orthonormal frame bundle $O(M)$...

Some thinking: Clearly, $x(p) = y(q) = 0$. If $\gamma$ is a geodesic, then it is easy to check $x(q) = -y(p)$. Denote $\rho = x(q) \in\mathbf R^d$. Then I believe that $x(m) = y(m) + \rho$ for all $m \in U$. If $\gamma$ is not a geodesic, then I think that a curvature term should appear, since we may use the holonomy. But I do not know how to prove my conjectures...

EDIT: I think there maybe no exact expressions for $x(m)$ and $y(m)$, but there should be an asymptotic expression with infinitesimals $o(\epsilon)$ or $o(d(x,y))$...
Comment: Indeed, there's not going to be an explicit description of $x(m)$ and $y(m)$. However, if you extend the definition of $x$ and $y$ to a map $x_t$, for each starting point $\gamma_t$. then you can probably derive equations that $\partial_tx_t$ satisfy. This is because $x_t$ is defined by families of geodesics, and the derivative with respect to the parameters that the geodesics depend on give you Jacobi fields, which satisfy the Jacobi equations. You can then study these equations.
Comment: @Deane I think I know what you mean by making use of Jacobi fields. I am now going to try it. Thank you!
Comment: Crosspost on [mathoverflow](https://mathoverflow.net/questions/404460/change-of-two-normal-coordinates-based-on-two-nearby-points).
Comment: @Deane Thank you professor Deane, for the nice hint. Yes, it is better to define $x(t) = u_t^{-1}(\exp_{\gamma(t)}^{-1}m)$. Then the problem is translated to find out the derivative $\frac{dx(t)}{dt}$. But still, I do not know how to use Jacobi fields to calculate this derivative...
Comment: @Deane Thank you professor. Inspired by your comments, I posted a partial answer to this general question.
Answer: Thanks for the instructive comments by @Deane, and by @WillieWong in the crosspost on mathoverflow.

First of all, the shift property $x(m) = y(m) + \rho$ that I conjectured in my question is not true in general. See the comments by @WillieWong for a counterexample.

Secondly, inspired by their comments, I am now trying to give a feasible option instead of a complete solution, as the question is actually quite general.
Denote
\begin{align*}
u_t &= \Gamma(\gamma)_0^t (u_p) \in L_{\gamma(t)}(M), \\
x_t &= u_t^{-1} \circ \exp_{\gamma(t)}^{-1} (m) \in \mathbf R^d, \\
V(t) &= u_t x_t = \exp_{\gamma(t)}^{-1} (m) \in T_{\gamma(t)} M, \\
\gamma_s(t) &=  \exp_{\gamma(t)}(sV(t)) \in M.
\end{align*}
Then for each $t$, $\gamma_\cdot(t)$ is a geodesic satisfying $\gamma_0(t) = \gamma(t)$ and $\gamma_1(t) \equiv m$. This leads to a Jacobi field $J_t$ along $\gamma_\cdot(t)$ for each $t$:
\begin{equation*}
J_t(s) = \frac{\partial}{\partial t} \gamma_s(t) = \dot\gamma_s(t),
\end{equation*}
which satisfies the Jacobi equation:
\begin{equation}\tag{1}
\frac{D^2}{ds^2} J_t(s) + R\left( J_t(s), \frac{\partial}{\partial s} \gamma_s(t) \right) \frac{\partial}{\partial s} \gamma_s(t) = 0,
\end{equation}
subjected to two boundary conditions:
\begin{equation}\tag{2}
J_t(0) = \dot\gamma_0(t) = \dot\gamma(t),
\end{equation}
and
\begin{equation}\tag{3}
J_t(1) = \dot\gamma_1(t) = 0.
\end{equation}
Now the torsion-freeness (which needs to be assumed in the beginning) gives
\begin{equation*}
\begin{split}
\frac{D}{ds} J_t(0) &= \frac{D}{ds} \frac{\partial}{\partial t} \gamma_s(t) \bigg|_{s=0} = \frac{D}{dt} \frac{\partial}{\partial s} \gamma_s(t) \bigg|_{s=0} = \frac{D}{dt} V(t) \\
&= \frac{d}{dh}\bigg|_{h=0} u_t\circ u_{t+h}^{-1} (V(t+h)) = \frac{d}{dh}\bigg|_{h=0} u_t x_{t+h} = u_t \dot x_t,
\end{split}
\end{equation*}
which gives
\begin{equation}\tag{4}
\dot x_t = u_t^{-1} \left( \frac{D}{ds} J_t(0) \right).
\end{equation}
Therefore, in order to obtain $\dot x_t$ which is exactly the purpose of the question, one need first solve the boundary value problem $(1)$-$(3)$ to get $J_t(s)$, and then calculate $\frac{D}{ds} J_t(0)$ and finally use $(4)$ to derive $\dot x_t$.


How do I use microprogramming to modify the instruction set architecture of an Intel CPU?

Question: Suppose I wanted to modify the implementation of an existing instruction. For instance, suppose I wanted to change the microcode that is executed by my processor when a "mov" instruction is executed after being fetched and decoded. How would I do that?
Also, suppose I wanted to add a new instruction to the ISA. For instance, suppose there is an opcode that isn't currently being used that I wanted my processor to be able to decode and execute after it is fetched. How would I do that?
Truthfully, I don't even know where to begin with this, which is why I'm asking on stack overflow. I don't have any experience writing microcode, and I only know the term from my computer architecture class. I know it's pretty easy to look up x86 instruction listings, but I don't know where I would even look up microinstruction listings. I don't even know if Intel makes their microcode listings public. I don't know what program I would need to use to load those new microprograms into the appropriate memory inside my CPU. I think the bootloader might be involved, but I could be totally wrong about that. If I own my CPU, I think I am legally able to modify its microcode and reverse engineer its instructions, but it would be easier to understand how my CPU works and how its microcode can be altered through existing documentation, rather than through reverse engineering.
I know there are some smaller hardware vendors that make open source ISAs--so maybe buying a CPU from one of those vendors would be good if I want to experiment with this? But I don't know what CPU from an alternative vendor would be the best for that. But really, I don't think buying a new CPU should be necessary for what I'm trying to do, since I could just use my existing CPU to run an open source ISA in a virtual machine if I had to (I'm interested in microinstructions and microprograms from a software perspective, not a hardware perspective).
Comment: Some reverse engineering of [AMD K8/K10 microcode](https://www.usenix.org/system/files/conference/usenixsecurity17/sec17-koppe.pdf) has been done, I'm not aware of such a thing for Intel cpus
Comment: The first thing you need is the documentation for the microcode execution engine in your particular processor (and it's model-specific; that after all is part of the point). Then you'd need the right microcode assembler, linker or similar mechanism to build a 'microcode image', and possibly a microcode loader.  But none of the necessary information is derivable from reading the public ISA.
Comment: As far as "running an open source ISA in a virtual machine" -- then the emulation of the ISA is the microcode.  For example, if you have an x86 emulator written in C, the 'microcode' language is C, and the microcode execution environment is all of the supporting code in the emulator.
Answer:
For instance, suppose I wanted to change the microcode that is executed by my processor when a "mov" instruction is executed after being fetched and decoded. How would I do that?

You'd:
a) Get a master's degree in Computer Engineering, Computer Science and/or Electrical/Electronic Engineering.
b) Apply for a job at Intel (or AMD), and get it
c) Work your way up the corporate structure to reach a position where you're able to influence the design of future products
Note that a simple mov instruction wouldn't use micro-code, so you'd have to change that first.

I don't have any experience writing microcode

Microcode is encrypted and treated as a trade secret; so "almost nobody on earth" (I'm guessing maybe 20 Intel employees?) has experience writing microcode for Intel CPUs.

I know there are some smaller hardware vendors that make open source ISAs--so maybe buying a CPU from one of those vendors would be good if I want to experiment with this?

"Open source ISA" mostly just means that a huge company that's able to spend many millions of $$ to manufacture their own chips saves about 50 cents on licensing fees.
The practical alternative is to use an FPGA (but the cost and performance is nowhere near what you'd get from a custom ASIC).
Answer: The various mov opcodes decode to a single uop in modern x86 CPUs (https://uops.info/ https://agner.org/optimize/), in fixed-function logic that can't be modified by microcode.  (Except mov to segment register, or mov to/from control or debug registers.)

Even Intel couldn't add arbitrary new instructions when they wanted to for Spectre mitigation; some of the decoder hardware is fixed-function, producing uops directly, only indirecting to the microcode sequencer for certain opcodes that need more than 4 uops.
What Intel did was add a new MSR (model-specific register).  wrmsr takes a number in ECX, and uses that number to figure out which microcode to run.  Like a "function pointer" except for the microcode.  So this new MSR was a way that a microcode update could add some new microcode to an old CPU, so OSes could get it to run and modify the CPU's internal state.
So ECX=49H (IA32_PRED_CMD) / wrmsr acts like a new instruction which you run for the side-effect, not to actually set a value.  See https://www.intel.com/content/dam/develop/external/us/en/documents/336996-speculative-execution-side-channel-mitigations.pdf

Similarly, to allow OSes to mitigate some MDS vulnerabilities by cleaning internal state at certain points, Intel added to the behaviour of verw, instead of adding a different instruction.  See https://security.stackexchange.com/questions/210209/what-are-the-new-mds-attacks-and-how-can-they-be-mitigated
Possibly some of the motivation was that the coding space for 32-bit opcodes is already full, but equally likely that it was already a microcoded instruction: More than 4 uops so it was handled by indirecting to the microcode sequencer.  So microcode updates can replace the sequence of uops that gets issued.
(64-bit mode has quite a few unused opcodes, as AMD64 removed some obsolete instructions like aaa and push/pop of segment regs.)

I don't even know if Intel makes their microcode listings public.

Very much no.  It's not only undocumented, the microcode blobs are encrypted and signed with a secret key.
The key for Goldmont has leaked, so its firmware can be decrypted and potentially reverse-engineered, although it won't be easy with zero documentation of the format or what the bits mean.  But still no way to load modified firmware into a real CPU, AFAIK.
Answer: If you are interested in custom instructions, check out RISC V.  It has opcodes reserved for customization, and being open source (hardware), can be edited without license or joining some private organization.


Use only WiFi connection (not cellular network) in app?

Question: My app has been rejected in the Apple app-store.
To fix this, it must use only WiFi and not cellular network connection when the user is using the application. The application uses UIWebView -- how can we go about implementing this restriction?
Any help will be appreciated,
Thanks!
Comment: Why was your app rejected for using cellular?  Lots of app use cellular data?  Curious because I'm going to be submitting an app that requires internet connection and I'd like to head anything off before I submit.  Were you doing something specific they objected to?
Comment: @pst: It depends on the application. I tethered my phone to my laptop recently because I didn't want to pay $15 per day for Internet access at a hotel, and blew out the data plan on my phone for the month, because Windows 7 decided to download a bunch of updates in the background.
Answer: Take a look at the Reachability example from Apple.
With that, you can query the device about the current network status, so you can know if Wifi is available, or if a connection will need cellular network. In the last case, you may display an alert, and prevent your UIWebView to load the data.
EDIT
If you have imported the reachability class into your project, and added the required framework(s), here's an example. This will test for an available WiFi connection:
<code>Reachability * reach;
NetworkStatus  status;

reachability = [ Reachability reachabilityForLocalWiFi ];
status       = [ reach currentReachabilityStatus ];

if( status == ReachableViaWiFi )
{
    /* Hurray, you've got a WiFi connection! */
}
else
{
    /* No WiFi connection - Alert the user! */
}
</code>
Comment: This is actually a tutorial. Download the sample app and try.
Comment: any tutorial how to implement only wifi in the app?
Comment: All you really need is the `Reachability` class, and a custom implementation of `-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;` as Macmade said.
Comment: I don't know how to do that. I started programming a few week ago. I imported the framework and the Reachability.h to my project. But don't know what I have to do now
Comment: thanks. I have added the framework and added the Reachability.h to my project. Is that correct? Now what I have to write to make the app use only wifi connection and not cellular data? Sorry, I'm new at this. Thanks a lot for your help!
Comment: I don't understant it. I have just to copy and paste it? Thank you very much for your help and sorry, I'm new
Comment: Well, just try... If you want to be a good programmer, you'll need to be curious some times...
Comment: Seems that cellular data is still active


Vue/Vuetify Data Table OData Support

Question: I have some odata-enabled endpoints that I'm looking to wire up to data tables in vue and have them automatically support server-side sorting, filtering and paging. The grid in Telerik's Kendo UI supports odata to the point where you tell the component that the data source type is odata and it automatically "just works" (https://demos.telerik.com/kendo-ui/grid/remote-data-binding) because odata is a well-defined standard.
I'm wondering if there is support for odata in other common vue-specific UI frameworks like vuetify. I've looked at vuetify specifically and it does seem to support server-side operations, but it's not clear to me how much custom logic I'm going to need to write in order to use odata since I haven't been able to find any specific examples.
Here is the source from the example linked above that shows how clean it is to wire up declaritively without any additional boilerplate logic:
        $(document).ready(function() {
            $("#grid").kendoGrid({
                dataSource: {
                    type: "odata",
                    transport: {
                        read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Orders"
                    },
                    schema: {
                        model: {
                            fields: {
                                OrderID: { type: "number" },
                                Freight: { type: "number" },
                                ShipName: { type: "string" },
                                OrderDate: { type: "date" },
                                ShipCity: { type: "string" }
                            }
                        }
                    },
                    pageSize: 20,
                    serverPaging: true,
                    serverFiltering: true,
                    serverSorting: true
                },
                height: 550,
                filterable: true,
                sortable: true,
                pageable: true,
                columns: [{
                        field:"OrderID",
                        filterable: false
                    },
                    "Freight",
                    {
                        field: "OrderDate",
                        title: "Order Date",
                        format: "{0:MM/dd/yyyy}"
                    }, {
                        field: "ShipName",
                        title: "Ship Name"
                    }, {
                        field: "ShipCity",
                        title: "Ship City"
                    }
                ]
            });
        });

Comment: This may or may not be helpful, but Kendo UI does have a [Kendi UI for Vue](https://www.telerik.com/kendo-vue-ui). I don't think Vuetify has odata support yet.
Comment: Yeah, sorry... I should have noted in the original post, but I'm going to be adding functionality to an existing vue application that currently uses vuetify. I'd like to avoid adding in a whole new UI framework like Kendo UI just to use a single component of it. We also want to have visual cohesiveness with the other tables (which call REST APIs that don't support odata and do all sorting, filtering, paging client-side) and they use vuetify data-tables.
Comment: I get it. I added an answer below with my suggestion on libraries to use to make this as simple as possible.
Answer: Vuetify doesn't seem to support odata yet, so your best bet is probably to write a client-side js solution.
Probably using one of these libraries.
O.js seems to be rather simple.

Vue/Vuetify Data Table OData Support


Which non-faction battleships have the best chance to get into an incursion fleet?

Question: I've never flown in an incursion, but I'm thinking about trying it out in the near future. From what I've heard incursion fleets are rather picky about which ships they take, with rather expensive navy or pirate faction ships being preferred.
I don't want to risk a billion or more for trying it out, I'd like to try it in a common T1 battleship first. I haven't had any use for a battleship so far and haven't invested any significant amount of training into any specific race or weapon system at the BS size.
From what I understand so far, missile ships are not in favor and shield tanking is far more common than armor tanking in incursion fleets.
Which battleships are in demand for incursions? Which one should I learn to have a good chance of getting into incursion fleets?
Comment: Have you popped into The Valhalla Project's channel in game? They should be able to help you with a good start and a fit for whichever BB then need more of.
Comment: You should take a look at [that website](http://incursions.nexsoft.de/), you'll find some usefull advices & links. (Most of it is about shield tanked ships, i don't have links for armor fleets atm...)
Answer: you can try Abbadon or Apoc also Mael is ok all should be t2 fitted (appart of guns) good EHP and omni res above 70%. If yuo can fit it this way you should be ok. Just don't expect to be picked up to fleet oftenly. Every FC will take any t2/t3 ship rather then t1 ofc.
Low cost for you should be navy version BS or Command Ship as a minimum. Sometimes t1 BS will be picked up for sniper role. But only if there is no t2 around.
Answer: My understanding is the sniper rokh is always useful if you want to try getting into incursions using a T1 ship. Obviously they will pick up faction battleships first. Just stay away from missiles, everyone in incursions always complains about missile boats because they take too long to get get damage on the target.
If you can't afford a faction battleship and just want to test out incursions hop in a logi ship. I always see this recommendation for pilots new to incursions. They are cheap, don't require a ton of training, and always highly sought after in fleets. This should give you an idea of how the incursion community functions and what will be expect of you when you're ready to invest in a faction BS.


Computer doesn't recognize mic on my headset on Ubuntu 20.04

Question: I upgraded to Ubuntu 20.04 recently from 18.04 and hard a hard time getting the computer to emit sound from the internal speakers, now i need to get it to register the microphone on my headset in order to speak in it.
Info i have:
It does detect the sound coming in the internal microphone, but it sounds like an airport.
My computer is an Acer Aspire 7741ZG
When typing cat /proc/asound/card*/codec* in the command line, it returns:
Codec: Realtek ALC272X
Codec: ATI R6xx HDMI

I tried methods from Headset microphone not working on Ubuntu 20.04 and got stuck when i needed to add the line to /etc/modprobe.d/alsa-base.conf
options snd-hda-intel model=???

I couldn't get the proper model of my chipset as it is not on this site https://www.kernel.org/doc/html/latest/sound/hd-audio/models.html
which bothers me is that i don't know where else to get it ?
Thanks for your help, have a nice day
Answer: Found the answer on https://forums.linuxmint.com/viewtopic.php?t=286654 where the guy just put auto in place of the chipset's name, thus my last line in /etc/modprobe.d/alsa-base.conf is
options snd-hda-intel model=auto

Now i can talk in class !!!


How to pull toothbrush out of the sink?

Question: My toddler dropped a toothbrush down a bathroom sink.  Unfortunately, the stopper wasn't in the sink at the time.
When looking down from above the sink, I believe the toothbrush is in the PVC section of the picture, and leaning against the side.  The PVC is a larger diameter so even if I could reach the toothbrush from above, I'd need to pull it center before pulling up.
How do I remove the toothbrush and get the sink functional again?
Comment: ..it looks like some landscaper just MacGyver'd the p-trap assembly out of spare parts and solvent. lol
Comment: Can you include a photo of the entire trap (the loopy part of the drain). Usually traps are designed to be taken apart for cleaning, and/or retrieving dropped items.
Comment: if the bristles were up it might have been easier to "snag". If you remove the plug at the bottom can you snake it through?  Either grab or push further down to the opening?  Remember to put a plastic tub underneath to catch the water in the trap.
Comment: Can you see the toothbrush down the drain?  Looks like it travelled pretty far.  If so, you can construct a small hook like contraption out of a wire hanger.  Ideally the bristles are up and you can make a small circle on the end, then flatten 90 degrees and wrap it around and grab it and lift up.
Comment: @Tester101 added a picture with bottom of trap.
Comment: @treeNinja Yes I can see it, but the bristle side is down.  I haven't tried a wire hanger yet, but that's an interesting suggestion, thanks!
Answer: In my part of the world, PVC P-traps for sinks and basins are universally designed so that you can easily remove the U-bend from below without tools.

I wouldn't hesitate very long before undoing all the obvious nuts visible in your picture† and attempting to gain access to the U-bend.
In some cases a strap-wrench may be useful, but usually those joints between PVC sections are sealed by a simple rubber ring and shouldn't have any kind of putty or sealant applied (which can make them hard to remove by hand)

† Obviously, except the one on the flexible tap connector(s)!
Comment: Same is true in my part of the world, but as @elrobis notes, this one looks (from what we can see in the picture) like a "custom" job. Might be time to De-Customize it back to something a bit more practical.
Comment: Agreed with @Ecnerwal: if you have to cut it apart to get the toothbrush out, take the opportunity to install a new standard trap where the U-bend is easily removable.
Answer: You could try using a pick-up or claw tool to reach down in the drain to grab it and pull it out.
Something like this one at Amazon.com.
Answer: Before you start taking things apart..... try this!
Get a dowel and your glue gun.  Dab hot glue onto tip of dowel, quickly put down drain onto toothbrush ( or whatever you've dropped down there) leave for a minute or 2 and pull dowel out!!
Nothing to take apart!  This probably would not work for a small item in the water but worth a try!!!
Comment: Hello, and welcome to Home Improvement. Thanks for the answer; keep 'em coming. And, you should probably [take our tour](//diy.stackexchange.com/tour) so you'll know how best to contribute here.
Comment: Heh, ended up paying a plumber to make a proper trap here.  But, very interesting suggestion, thanks :)

How to pull toothbrush out of the sink?


Is it OK to drain sump water into a pond?

Question: My current sump system drains into an area that has high traffic for various activities such as log splitting and brush clearing. This creates a very muddy mess. I want to relocate the sump drain pipe to a pond that is close by (< 50 ft.). Is this acceptable to do? The pond water does have the potential to seep into a creek nearby as well. I am assuming it is OK since the water is from the ground/rain but I want to be sure it would be appropriate.
NOTE: The pond is on my private property. The water does not actively drain into the creek unless there is a lot of rainfall in the spring. And even then, there is about 100' until the water reaches the actual creek (the creek branches out towards my pond).
Comment: You'll likely have to contact your local government, as this is going to be different depending on where you are. This could vary from town to town, city to city, state to state, country to country.
Answer: Unless you have reason to believe that the sump output is contaminated, and assuming that you have legal access to the pond, this is a perfectly viable solution. In fact, even if the pond does eventually (or occasionally) drain into a stream, the natural filtering effect of the process should remain somewhat intact.
As Tester101 points out, local and state laws may apply. In my experience there's no problem ejecting groundwater into a wetland in a non-destructive manner. We've run open-loop geothermal systems into adjacent wetlands, and in Minnesota wetlands are strictly protected.
Comment: The pond is on my land. There is also no reason that the water should be contaminated.


Javascript - onload event

Question: I am having a problem with onload event when I am opening new tabs without switching to them.
I am trying to write a user script for Chrome that adds "Copy" buttons after pre tags. It works fine when the page I am trying to load is on focus but when I open a new tab in the background, it isn't working.
Is this behaviour expected?
Here's my code:
// ==UserScript==
// @author       Vamsi Krishna | email@example.com
// @description  Adds a copy button after 'pre' tags.
// @grant        none
// @homepage     https://github.com/krikx/CopyCode
// @match        *://*/*
// @name         CopyCode
// @version      0.1
// ==/UserScript==

var css = "/* google fonts */ @import url(https://fonts.googleapis.com/css?family=Open+Sans); .kxbt { border-radius: 4px; cursor: pointer; display: inline-block; font-family: 'Open Sans', 'sans-serif'; font-size: 15px; margin-bottom: 5px; padding: 5px; padding-top: 3px; transition: .2s; } .blue { color: #55acee; border: 1px #55acee solid; } .blue:hover { background-color: #55acee; color: #fff; } .green { color: #2ecc71; border: 1px #2ecc71 solid; } .green:hover { color: #fff; background-color: #2ecc71; } .red { color: #e74c3c; border: 1px #e74c3c solid; } .red:hover { color: #fff; background-color: #e74c3c; } /* green and red are almost never used as timeout is too fast to observe any noticible difference */";
var style = document.createElement('style');
style.innerHTML = css;
document.getElementsByTagName('head')[0].appendChild(style);

function Init(){
    var blocks = document.getElementsByTagName('pre');

    for (var id = 0; id < blocks.length; id++){
        var btn = document.createElement('div');
        btn.innerText = 'Copy';
        btn.setAttribute('class', 'kxbt blue');

        InsertAfter(blocks[id], btn);
    }
}

function InsertAfter(refNode, newNode){
    refNode.parentNode.insertBefore(newNode, refNode.nextSibling);
    WireClick(newNode, refNode); // add the click funtionality
}

function WireClick(btn, block){
    btn.addEventListener('click', function(){
        Copy(btn, block);
    });
}

function Copy(btn, block){
    var range = document.createRange();
    range.selectNode(block);

    var sel = getSelection();
    sel.removeAllRanges(); // TODO | removing selection without backup
    sel.addRange(range);

    var success = document.execCommand('copy');
    sel.removeAllRanges(); // possible TODO
    var msg = success ? 'Copied!' : 'Error!';
    var tcls = success ? 'kxbt green' : 'kxbt red';

    btn.innerText = msg;
    btn.setAttribute('class', tcls);
    setTimeout( // revert back to blue in 256ms
        function(btn){
            btn.innerText = 'Copy';
            btn.setAttribute('class', 'kxbt blue');
        },
        256, btn
    )
}

function main(){
    var body = document.getElementsByTagName('body')[0];

    // do not inject twice
    if (body.hasAttribute('data-kxbt-inited')){
        return;
    }

    body.setAttribute('data-kxbt-inited', true);
    Init();
}

// wait until ready
window.addEventListener('load', main);

Comment: "it isn't working" You mean the buttons are not added?
Comment: @Teemu, yes. Buttons are not being added.
Answer: Adblock Plus probably was the cause of issue.
It is working properly after disabling and re-enabling ABP.

As I still don't know the cause of issue or why (apparently) disabling/re-enabling ABP solved the issue, it'd be great if anyone can help.

Javascript - onload event


Why I'm Getting an error of the argument AwsService at index [1] is available in the TestModule context

Question: Here we are getting the Error the File which named as "Test.module.ts
Comment: did you read the FAQs page on nestjs docs?
Comment: I just got the Answer. i was little bit confused About that . It was My Mistake..
Comment: Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
Answer: we need to Add the Module Name which we used for the AWsService.Like i am using the Aws From Shared module and for using it into an other component i just import the name "SharedModule"
Comment: As it's currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer).


Making a roulette game, keep getting error in python

Question: I'm fairly new to python, so sorry if this is a bit basic.
I have undertaken making a roulette game to improve my programming skills and have come across a very strange error. At first, when I wanted to print the player's balance at the end of the roll and once the results were checked, it printed out the array that holds all the red numbers. I looked over the code that was defining the balance variable but there was nothing that looked like it might affect it.
I for some reason thought that defining balance as int(100) instead of 100 might fix it but i instead got this error:TypeError: checkresults() missing 1 required positional argument: 'bet' I deleted the int in balance and it still persisted. For those wondering, I make checkresults use bet and have even tried moving the place where it goes in the list of variables used but still no fix. balance = checkresults(bet, bet_choice, number, balance, red, black, green, first, second, third) as you can see, it's the first variable it uses. The full code for checkresults is here:
def checkresults(self, bet_choice, number, balance, red, black, green, first, second, third, bet):
if bet_choice == "number":
    if bet == number:
        balance = balance + (bet_amount*14)
        print("You Won!")
    else:
        print("You Lost")
elif bet_choice == "colour":
    if bet == "red":
        if number in red:
            balance = balance + (bet_amount*2)
            print("You Won!")
        else:
            print("You Lost")
    elif bet == "black":
        if number in black:
            balance = balance + (bet_amount*2)
            print("You Won!")
        else:
            print("You Lost")
    else:
        if number in green:
            balance = balance + (bet_amount*14)
            print("You Won!")
        else:
            print("You Lost")
elif bet_choice == "third":
    if bet == "1st":
        if number in first:
            balance = balance + (bet_amount*3)
            print("You Won!")
        else:
            print("You Lost")
    elif bet == "2nd":
        if number in second:
            balance = balance + (bet_amount*3)
            print("You Won!")
        else:
            print("You Lost")
    elif bet == "3rd":
        if number in third:
            balance = balance + (bet_amount*3)
            print("You Won!")
        else:
            print("You Lost")
return balance

the value for bet is decided by this:
def choosebet():
bet_choice = input("Would you like to bet on a number, colour, or a third? ")
if bet_choice == "number":
    bet = int(input("Which number would you like to bet on? "))
elif bet_choice == "colour":
    bet = input("Which colour would you like to bet on?")
else:
    bet = input("Which third would you like to bet on?")
return bet, bet_choice

Comment: It would help if you could post what values are being fed into checkresults().
Comment: It seems that you have a mandatory `bet` argument at the end of your function declaration that you are not passing a value to.
Comment: Ohad Chaet i put a print statement in front of the checkresults and bet does have a value as it printed correctly
Comment: Hi @RandxmStranger, is very important ask questions in the right way in order to get a high quality response. As @Joshua and @Ohad suggest, you have a problem with the  `checkresults` call. For that we suspect that here is not important the body of this function. Please edit your answer including the `checkresults` call.
Answer: I had self in the defining part of the checkresults code, from a previous error. I deleted self and 1: it fixed the new error and 2: the old error didn't come back.
Answer: checkresults as defined takes eleven arguments - it looks like the first parameter is supposed to be an instance of the class that checkresults is defined in - when you call the method directly without reference to the instance it thinks you did not pass the eleventh argument.
Answer: I am doing the exact same thing. I started python about two and a half months ago, and have been working on a roulette game for the past month.
This doesn't answer the question, but I had similar issues when I started. If your intent is to build out the entire game, the path you are on is not sustainable. There will be too many elif statements and you will want to rip your hair out (as I did).
What I ended up doing was putting all the "constants" in dictionaries. By constants I mean the numbers in each bet (red vs black, odd vs even, etc), the numbers on the wheel, and the payouts for each bet.
From there you can use dictionary methods to lookup up the winning numbers in the bet dictionaries (i.e. is this a red or black number) and then compare that to a dictionary of placed bets, and if there is a match, look up the payout for the bet.
Good luck!


Errors When Configuring Samba For Better MacOS Performance

Question: I've been following this article to try optimize my samba setup for macOS and enable me to backup to it via TimeMachine.
Implementing the suggestions at the bottom of the article I'm still able to read from my share and copy data too it but am unable to create new folders or rename existing ones. As seen in the screenshot I'm given this error when trying to create a folder called "hello" but it reverts to "untitled folder". The folder is actually created and exists the remote share but the name doesn't stick.

I get another error when trying to delete the folder which can be seen in the image below.

If I try to copy over a text file on its own as seen in the screenshot below I receive a different error. The file is copied over and retains its name but the contents are lost.

If I place that same file in a folder and copy the whole folder over there is no error and both the folder and file retain their names and the file retains its contents.
However, if I try to delete this folder containing the text file I am confronted with the error found below. I have checked the file and it is not locked and my current user has read-write permissions (as seen in finders get info).

Here is my current smb.conf file:
[Global]
log file = /var/log/samba/log.%m
max log size = 1000
logging = file
server role = standalone server
obey pam restrictions = yes
unix password sync = yes
passwd program = /usr/bin/passwd %u
passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccefully* .
pam password change = yes
map to guest = bad user
min protocol = SMB2
vfs objects = catia fruit streams_xattr
fruit:metadata = stream
fruit:model = RackMac
fruit:veto_appledouble = no
fruit:wipe_intentionally_left_blank_rfork = yes
fruit:delete_empty_adfiles = yes

[Media]
path = /mnt/hdd2tb/Media
writeable = yes
vfs objects = catia fruit streams_xattr

[Time Machine]
path = /mnt/hdd2tb/Backup
writeable = yes
vfs objects = catia fruit streams_xattr
fruit:time machine = yes
fruit:time machine max size = 1T

I've found that if I remove streams_xattr from the vfs objects field under my Media share it fixes all these issues but read here that it must be in the vfs stack in order for the fruit module to work.
I thought this might be a permission issue so ran sudo chmod -R 777 hdd2tb on my server which is the directory that I have my hard drive mounted. The effects of this can be seen below.

I've run out of ideas at this point so would be super grateful for any input, I'd really like to get this working.
Comment: There's some conflicting sentences, it makes it hard to understand what you actually want.  Do you want Time Machine to backup via SMB to the Media share? If not, why does it matter if `streams_xattr` is on Media or not?
Comment: @jnovack I have the Media share for my movies and stuff which I would also like to be optimized for mac which is why I have the vfs objects listed there also. Then I have the Time Machine share which is defined as a separate share with a size limit which I would like to use exclusively for backups via SMB. Does the make sense or clarity things?
Answer: I ended up fixing this by reformatting my hard drive to ext4 because it supports extended attributes which I wasn't aware of.


How can I assign a specific address to a variable?

Question: How can I assign a specific address to an already stored variable?
#include <stdio.h>

void main()
{
    int a = 10;
    printf("%d\n%d\n", a, &a);
    &a = 2300000;
}

Comment: @LưuVĩnhPhúc No void main() is not incorrect, where did you get that idea from? Read the link you posted, for example the answers by Jonathan Leffler or me. Most other answers are incomplete, far too simplified or just plain bad.
Comment: 1. [`void main()` is incorrect](http://stackoverflow.com/q/204476/995714). It's a relic from a non-standard compiler. 2. To print addresses use [`%p`](http://stackoverflow.com/q/9053658/995714). Using incorrect format specifier invokes undefined behavior
Comment: @LưuVĩnhPhúc main can be implementation defined. Thus void main is a valid main function. See the current standard: 22.214.171.124.1 Program startup
Comment: @2501 There's not even any indication that this question is about a hosted system. 126.96.36.199.1 refers to hosted systems only. Which indeed may have implementation-defined forms, though the standard is unclear.
Answer: You cannot change the address of a variable.The compiler does have facilities to assign an absolute memory address to a variable. Using pointer you can only point to some address. Like,
int *p;
p = (int*) 0x00010000;

Comment: Don't you need a cast to avoid a compiler warning?
Answer: No, there is no way by which you can assign an address to a variable. You can assign an arbitrary location ie.,  you can point to some address with a pointer like
int *ptr;
ptr = (int*)7000;

But changing or assigning a specific address is not possible.
Answer: The memory addresses that you see are not in fact actual physical memory addresses, but virtual addresses. every process receives its own virtual memory space and it is possible to have variables in a few processes with the same "address".
therefore changing the address cannot be done, and it is also meaningless to do so.
in unix, you can use posix_memalign to allocate an address that is aligned to a specific number, but it cannot be any address that you want, that is because C automatically aligns memory(for example padding of structs).
memory can only be aligned to a number that is a power of 2.
Answer: You can actually set a specific address to a variable the following way:
int * address = (int *)0xADD4388; // some arbitrary address
int & refvar = *address;
cout << "refvar: " << (size_t)refvar << ", &refvar: " << (size_t)&refvar << endl;
refvar = 4; // assignment will write to a specific address

Comment: This looks like C++.  C and C++ are different languages.
Comment: @Andrew-Henle, you are right. I did not notice "c" tag. But I had a similar question in C++ and a lot of embedded systems now have C++ compilers.


Looking for JQuery drag and drop features

Question: Here's what I want to achieve.
I want to be able to have a list of words, which I can drag over an input field and drop into the value. I want to be able to do this multiple times possibly with the same word, to go in more that one input field.
Basically I'm sorting words into groups.
If anyone could point me at a JQuery library that can do this sort of thing, that would be great.
Cheers,
Thomas.
Answer: Have a look at jQuery UI's draggable and droppable functions:
http://jqueryui.com/demos/draggable/ and http://jqueryui.com/demos/droppable/
Comment: The droppable function seems to stop working when I select the html object by class and not ID.
Comment: @diggers It should work. Check over your codes carefully. If you still can't solve it you can always post a question here.


Is it appropriate to display the warning message "Caps Lock is on"?

Question: Is it appropriate to display the warning message "Caps Lock is on" when focus shifts to Password field?
In gmail and yahoo I don't see it. So, what does the standard say?
Answer: What standard? Standard for Safari, Mac, Windows, Chrome, w3c, iso ...?
Some browsers do this automatically as 'standard'.
Some websites add the functionality so it's available in all browsers.
But it's not always possible (in all browsers on all platforms) to detect that caps lock is on whilst visiting a website although there is a workaround that requires the user to have already entered some text (based on the premise that if the user presses A, but shift was not down at the time, then caps lock must be on).
There's no standard that says you should do this, but I believe it's generally (ie not 100%!) accepted as a good thing to have, as its usefulness outweighs its existence.
Comment: sorry - I was going to use the term 'cognitive load' but inadvertently ended up using something less meaningful and didn't really finish the sentence properly :-)   My point there was that if and when you do happen to see this in action, then the chances are that it's going to be useful. You *might* deliberately have caps lock on when entering a password - but for those cases when you do (and therefore maybe see the warning as unnecessary), the occurrence of those situations is *more than outweighed* by the number of *useful cases* where you did not mean to have caps lock on.
Comment: Good answer, but I don't really understand "its usefulness outweighs its existence". What do you mean by that? Did you mean another word where you have existence? That would seem to make more sense to me.
Comment: That's what I thought you meant, and that's my thoughts on it exactly.
Comment: @Roger @Charles How about "its utility outweighs its cost (in cognitive load)"? :)
Answer: I haven't heard of a standard either, but I really like the way it is done in Mac OS X. It is very clear.

In Windows, there's a warning text message. While this might be easier to understand, it is more obtrusive.
Comment: There's a reason it is more obtrusive. That little icon in Mac OS means absolutely nothing to me as a user. The warning message in Windows is much better *because* it stands out. Caps lock is something that many people don't notice because in general, they aren't observant of the little things. That icon is another example of a "little thing".
Comment: +1, and the Windows message (if you mean the one at logon) only appears when the password is false, which is good imho because there are cases where the usage of caps lock is intended by the user (e.g. if his password only contains uppercase letters).
Comment: Most people don't leave CAPSLOCK on very often, so it really doesn't annoy them very much.
Comment: But the Mac OS X (it didn't exist in Mac OS in the 90s) way is an idiomatic interface pattern. First time you see it you have to understand it, but most people do by trying, and then it will never bother you again, just help you. The Windows way is disturbing me every time.
Comment: @Matt - nope, the windows way appears as soon as you hit the password field with capslock on - I just checked Windows 7, Vista and Server 2008. And that is not a very good icon at all, because I had to zoom in quite a bit to be able to tell that was an arrow and not an exclamation mark. The fact that it is not clear is more evidence that it is not a good indicator.
Comment: @Charles - I agree with Matt here (of course :) ... it is very obvious when you use it. The Windows way is a yellow warning sign, which I find slightly offensive, like blaming me for forgetting caps lock. The caps lock arrow on Mac feels like a nice reminder.
Comment: @Martin - most people wouldn't even notice that little icon the first time and are likely to keep typing their password in over and over again, not realizing that capslock is on. And how is the Windows way disturbing you every time? Do you keep your capslock on all the time?
Comment: @Charles - A lot of people at my workplace and elsewhere, including myself, have caps lock on often. Doing it the Windows way could be bad, because some sites have policies on the number of times you can get the password wrong before a lock out, and I have fallen victim to this a few times. Say you vaguely remember your password, you might need that extra try to get it right. With the icon in OSX it is easy to notice, and a widely accepted icon for upper case.
Comment: @Matt - with limited retries, wouldn't the Windows way be *better*? You clearly know that you have capslock on and won't waste one of those retries dealing with capslock. And since when is an exclamation mark a "widely accepted icon for uppercase"? I've never seen that used as an icon to represent uppercase, capslock or anything else related.
Comment: @ Charles - It's an up arrow. It's all over the place. All over Apples iOS and OSX, as well as tons of web apps/sites. And the Windows way appears AFTER you failed your first try.
Comment: @Charles - I can see it clearly and to be far the screenshot is smaller and horizontally skewed, as opposed to how it actually appears in real life.
Comment: @Charles - You are right about how it occurs on Windows, I was just going off of @wildpeaks's recollection.
Comment: +1 This is definitely the most simple and elegant solution.
Answer: You might want to do this, but only if the user has guessed his password wrong once already -- I believe this is how Windows normal login works, or worked at some point, at least. This way, you can protect users who use the caps lock key to type in their passwords (intentionally) from people with wandering eyes.
Answer: Instead of displaying a warning when Caps-Lock is on you could switch the letters if the first password fail first checking pASSWORD1234 and then Password1234 allowing the user to login as the user did input the right password, just mistakenly with a locked in modifier.
As Patrick McElhaney's edit to this answer clarified I'm not at all suggesting we should make our passwords case-insensitive. pASSWORD1234 would work, PASSword1234 and PassworD1234 would not.
The reason why this isn't really less secure is that for starters you would simply hash both pASSWORD1234 and Password1234 and check if they match your salted, crypto-strength hashed password in your database.
We are essentially giving the users 2 guesses per 1 input, but that doesn't mean this approach is vulnerable to brute forcing as the chance of guessing someones password after 100 attempts is minuscule to begin with. After you've had a large amount of bad guesses you would normally lock the user out either way.
Comment: So, let's make passwords less secure as a way to get around capslock? Sorry, but no.
Comment: @Anirvan - In anything I know I would just ask it to do login(password) and then login(invert(password)) to see which works. Alternatively wrap the default login which is about as much work. I'm not sure what it would be a lot of work to implement in.
Comment: @Patrick, I think what Charles was saying was that in secure password management systems, the service provider doesn't actually know what the password is, only the hash of the password. (This is why well-designed services will let users who forget their password _reset_ their password, rather than email it to them; you can't mail what you don't have.) Implementing your idea would mean going against a major security best practice.
Comment: @Anirvan That doesn't matter. The client would hash two versions of the password and try each of them. .
Comment: @Patrick, that makes sense, I guess. It's still a lot of work to do at any scale, and increases the chance you'd mess something up on the security side (because you couldn't rely on standard pre-built authentication frameworks anymore).
Answer: It really depends on your users in my opinion.  If you are seeing a lot of users have multiple views on he login page, or if it's possible in your system, the validation error message page more than what you would expect then there may be something holding them back from logging in. It could be the caps lock or it could be something else.
I would look at your stats, and then decide if you are going to implement something.  If you do go with the caps lock approach, implement it and then review stats again for a month or so and see if the hits on the pages has reduced to be closer to the unique visits.  If it has increased or unchanged (which with this change I would think it would decreases) then maybe the caps lock solution isn't helping and another change is required.
I guess all I am saying is that if you are going to do this incremental changes that may or may not be a web standard then benchmark your stats first and then review.


How to isolate $\,\alpha\,$ given $\,A\!\cdot\!\sin(\alpha) + B\!\cdot\!\cos(\alpha) = C$

Question: I encountered this problem while trying to generate a truncated cone tangent to a sphere given the sphere's radius, the minor perimeter's radius, and the distance between center points. Isolating this angle is useful as it will tell me the second radius and distance in one variable. I have attempted to isolate α using the following equation:
$\alpha=\arctan\left(\dfrac CB\right)-\arccos\left(\!\!\dfrac A{\sqrt{C^2 + B^2}}\!\!\right)$
This equation was derived geometrically, but it does not work in practice. Changing the form to account for possible gaps in the domain has not worked, so I'm assuming my methodology is flawed.
Please let me know if there is any need for clarification.
Answer: Let $\theta$ be an angle such that $\cos(\theta) = A/\sqrt{A^2+B^2}$ and $\sin(\theta) = B/\sqrt{A^2+B^2}$ (so if $B \ge 0$ you can use $\theta = \arccos(A/\sqrt{A^2+B^2}$, but if $B < 0$ you'll want $-$ that).  Then your equation says
$\sin(\alpha+\theta) = C/\sqrt{A^2+B^2}$. Can you continue?
Comment: This would then allow for isolating + by taking the arcsin of the new equation - subtracting the identity for  then isolates . Is that correct?